umbrella: [rdom] unexpected dispatch $replace after change route / reload layout

https://github.com/thi-ng/umbrella/assets/42169423/d887122f-e34e-4555-b044-84a956a66144

// DropDown

import { $replace } from '@thi.ng/rdom'
import { fromViewUnsafe } from '@thi.ng/rstream'
import { states$, toggle, useSlot } from '../../modules/index.js'
import { Menu } from '../index.js'


const slot = x => {
  const { state } = x

  return [
    'dropdown',
    {
      onpointerdown: toggle(x)
    },
    ['button', { type: 'button' }, 'Drop Down'],
    state
      ? Menu(x)
      : null
  ]
}

export const Dropdown = seed => [
  'control', {},
  $replace(fromViewUnsafe(states$, { path: useSlot(seed), tx: slot }))
]

// Menu


import { $list } from '@thi.ng/rdom'
import { cancel, end, start } from '../../modules/slot.js'
import { Icon } from '../index.js'

const Item = ({ ico, label }) => ['li', {}, Icon({ id: ico }), label]

export const Menu = x => {
  // LOG('MENU:RDR')

  const { id, state, placement, data$ } = x

  return $list(
    data$,
    'menu',
    {

      id,
      state,
      placement,

      onanimationstart: start(x),
      onanimationcancel: cancel(x),
      onanimationend: end(x)
    },
    Item
  )
}
// State from Atom


export const states$ = defAtom({})

//  useSlot


import { defCursor } from '@thi.ng/atom'
import { states$ } from './hub.js'


export function useSlot (seed) {
  const { id } = seed
  
  if (!Reflect.has(states$.deref(), id)) {
    const cursor = defCursor(states$, id)
    seed.placement = 'bottom'
    seed.state = 'hidden'

    cursor.reset(seed)
  }
  return id
}

// Page / Layout 

import { data$ } from '../../modules/index.js'
import { Dropdown, Top } from '../index.js'

export const Dashboard = () =>
  [
    'main', {},
    Top(),
    ['header', {}, 'DASHBOARD'],
    [
      'section', {},
      [
        'bar', {},
        Dropdown({
          id: 'x1',
          data$
        })]]

  ]


About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 16 (8 by maintainers)

Commits related to this issue

Most upvoted comments

@bit-app-3000 that’s very helpful to learn & a great use case — thank you!

I’ve updated the $compile() function to add checks for embedded function forms, call the function and then only compile the result… I’m doing some more testing and then release asap (your example above has no more problems now, as far as i can tell!)

Those should be working like this:

const myComponent = (...items: any[]) => [
	"div",
	{ class: "custom" },
	...items.map((x) => x.toUpperCase()).join(", "),
];

$compile([myComponent, "yabba", "dabba", "doo"]).mount(document.body);

Zero-arg functions in child/body positions… (only supported since rdom v1.5.0, released just now)

const random = () => ["li", {}, Math.floor(Math.random() * 100)];

$compile(["ul", {}, random, random, random]).mount(document.body);

@bit-app-3000 I just updated the example with some more features and more comments (also requires a newer version of thi.ng/router, just published) 😃 Hth!

Hi @bit-app-3000 — so because your project has way too many files and I also couldn’t get it to run locally, I still couldn’t figure out what is going wrong in your case, but I’ve just created & uploaded a new example with a similar setup/task, i.e. a router & atom-based component switcher (incl. dynamic/reactive lists of images). I hope this much more stripped down example will help you figure out what might be wrong on your end, but I’m afraid that’s all I can offer you here… obviously always happy to answer any other related questions

Demo: https://demo.thi.ng/umbrella/rdom-router/

Source: https://github.com/thi-ng/umbrella/blob/develop/examples/rdom-router/src/index.ts

Really hope that helps!