-
Currently seems that useMenuBar and useMenuState do not share state |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 5 replies
-
What's the use case? Can you provide an example or CodeSandbox of what you're trying to do? |
Beta Was this translation helpful? Give feedback.
-
Another issue is that could not succeed passing another props to As component /** @jsx h */
import { VNode, h, Fragment } from 'preact'
import styles from './facets.style.scss'
import cs from 'clsx'
import { useMenuBarState, MenuBar, MenuItem } from 'reakit/Menu'
import {
CheckboxFilter,
CheckboxFilterData,
CheckboxFilterDataSelected,
} from '~/components/Filter/CheckboxFilter'
type FacetsData = { id: string; label: string; data: CheckboxFilterData }[]
type FacetsDataSelected = { id: string; data: CheckboxFilterDataSelected }[]
export type FacetsProps = {
data: FacetsData
onChange: (data?: FacetsDataSelected) => void
}
export const Facets = ({ data, ...props }: FacetsProps) => {
const menu = useMenuBarState({ unstable_values: { a: 1 } })
return (
<MenuBar {...menu} className={styles.menuBar}>
{data.map(({ id, label, data }) => {
return <MenuItem key={id} id={id} label='label' {...menu} as={CheckboxFilter} />
})}
</MenuBar>
)
} /** @jsx h */
import { VNode, h, Fragment } from 'preact'
import styles from './checkboxFilter.style.scss'
import cs from 'clsx'
import { Button as RKButton, ButtonHTMLProps } from 'reakit/Button'
import { Ref, useRef, FC, forwardRef, Component, useEffect, useState } from 'preact/compat'
import { WithChildren } from '~/utils/typeUtils'
import { useMenuState, Menu, MenuItem, MenuButton, MenuSeparator, MenuItemCheckbox } from 'reakit/Menu'
import { ReactComponent as Svg } from '~/assets/arrowdown15.svg'
import { Button } from '~/components/Button/Button'
export type CheckboxFilterData = { id: string; value: string }[]
export type CheckboxFilterDataSelected = string[]
export type CheckboxFilterProps = ButtonHTMLProps & {
data: CheckboxFilterData
onChange?: (data?: CheckboxFilterDataSelected) => void
label: string
}
export const CheckboxFilter = forwardRef(
({ label, data, onChange, ...props }: CheckboxFilterProps, _ref: Ref<HTMLButtonElement>) => {
const menu = useMenuState()
let { unstable_setValue, unstable_values } = menu
useEffect(() => {
onChange?.(unstable_values.values)
}, [unstable_values, onChange])
let ref = useRef<HTMLButtonElement>(null)
useEffect(() => {
if (menu.visible) {
ref.current?.focus()
}
}, [menu.visible, ref])
return (
<div class={styles.select}>
<MenuButton {...menu} {...props}>
{(props) => {
return (
<Button {...props} RightIcon={Svg}>
{label}
</Button>
)
}}
</MenuButton>
<Menu className={styles.menu} {...menu} aria-label={`filter ${label}`}>
{data.map(({ id, value }, index) => {
return (
<MenuItemCheckbox
ref={index === 0 ? ref : undefined}
key={id}
id={id}
className={cs(styles.checkbox_item)}
name='values'
{...menu}
value={id}
>
{value}
</MenuItemCheckbox>
)
})}
</Menu>
</div>
)
}
) That label gives gives error So I tried passing everything through unstable_values... |
Beta Was this translation helpful? Give feedback.
-
I don't really recommend using Different state hooks don't share state by default, but you can bubble state changes up the same way you would do with any React local state: https://codesandbox.io/s/reakit-menu-sharing-values-4c3iu |
Beta Was this translation helpful? Give feedback.
-
I notice @Diegohas that passing {...menu} creates needless rerenders. |
Beta Was this translation helpful? Give feedback.
I don't really recommend using
unstable_values
right now. If you do, make sure you follow the instructions on https://reakit.io/docs/experimental/Different state hooks don't share state by default, but you can bubble state changes up the same way you would do with any React local state: https://codesandbox.io/s/reakit-menu-sharing-values-4c3iu