Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throw when passing conflicting props to store #2745

Merged
merged 4 commits into from
Aug 24, 2023

Conversation

diegohaz
Copy link
Member

@diegohaz diegohaz commented Aug 23, 2023

This PR introduces an error that's triggered in the following scenario:

const a = useDialogStore();
const b = useDialogStore({ store: a, defaultOpen: true });

Both a and b ought to be synchronized. By default, the last store in the call stack takes precedence. However, it's still unclear what the value of the open state should be in the example above.

a has the open state set to false by default, while b has a default open state set to true. The default state should only be used when the open state is undefined. Since we're passing a to b and a has the open state defined, the defaultOpen value is disregarded, resulting in both a and b having the open state set to false.

This could be confusing, and we might change this behavior in the future. For now, we're throwing an error to prevent users from relying on any specific behavior until we make a decision.

If you have a particular use case that prefers one behavior over another, please submit a feature request.

@changeset-bot
Copy link

changeset-bot bot commented Aug 23, 2023

🦋 Changeset detected

Latest commit: cdeea1a

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 4 packages
Name Type
@ariakit/react-core Minor
@ariakit/core Minor
@ariakit/react Minor
@ariakit/test Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@vercel
Copy link

vercel bot commented Aug 23, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated (UTC)
ariakit ✅ Ready (Inspect) Visit Preview Aug 23, 2023 11:55pm
1 Ignored Deployment
Name Status Preview Updated (UTC)
reakit ⬜️ Ignored (Inspect) Visit Preview Aug 23, 2023 11:55pm

@codesandbox-ci
Copy link

codesandbox-ci bot commented Aug 23, 2023

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit cdeea1a:

Sandbox Source
Ariakit Configuration

Copy link
Contributor

@DaniGuardiola DaniGuardiola left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good and useful, but I noticed the scope seems to have been reduced from what we had discussed. We talked about how defaultValue is ignored when syncing two stores like this:

const store = useStore()
const store2 = useStore({ store, defaultState: true })

However, this PR only targets situations where value is set, which misses this case.

Can we check for that too? Am I missing something?

@diegohaz
Copy link
Member Author

@DaniGuardiola, do you have a specific use case in mind that this PR doesn't address?

Take a look at this example: https://codesandbox.io/s/ariakit-conflicting-store-props-error-ym4w5s?file=/index.js:130-241

The error occurs because the open state is initially set on the first store (it defaults to false).

Copy link
Contributor

@DaniGuardiola DaniGuardiola left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@diegohaz oh, my bad, I had misunderstood the code in throwOnConflictingProps and I though the value !== undefined here was checking for a (non-default) value among the props. After re-reading I realize this is not the case.

Looks pretty good to me! Only nit I can think of is that I tend to prefer thing.length === 0 to !thing.length since it's clearer what's going on, but that's just a personal preference.

@diegohaz diegohaz merged commit 5d3661e into main Aug 24, 2023
14 checks passed
@diegohaz diegohaz deleted the store-throw-on-conflicting-props branch August 24, 2023 19:00
@github-actions github-actions bot mentioned this pull request Aug 24, 2023
diegohaz pushed a commit that referenced this pull request Sep 6, 2023
This PR was opened by the [Changesets
release](https://github.com/changesets/action) GitHub action. When
you're ready to do a release, you can merge this and the packages will
be published to npm automatically. If you're not ready to do a release
yet, that's fine, whenever you add more changesets to main, this PR will
be updated.


# Releases
## @ariakit/core@0.3.0

### Minor Changes

- [`#2783`](#2783) **BREAKING**
_(This should affect very few people)_: The `select` and `menu` props on
`useComboboxStore` have been removed. If you need to compose `Combobox`
with `Select` or `Menu`, use the `combobox` prop on `useSelectStore` or
`useMenuStore` instead.

- [`#2745`](#2745) Component
stores will now throw an error if they receive another store prop in
conjunction with default prop values.

### Patch Changes

- [`#2782`](#2782) Fixed form
store not synchrozining validate and submit callbacks with another form
store through the `store` property.

- [`#2783`](#2783) Component
store objects now contain properties for the composed stores passed to
them as props. For instance, `useSelectStore({ combobox })` will return
a `combobox` property if the `combobox` prop is specified.

- [`#2785`](#2785) Added `parent`
and `menubar` properties to the menu store. These properties are
automatically set when rendering nested menus or menus within a menubar.

Now, it also supports rendering nested menus that aren't nested in the
React tree. In this case, you would have to supply the parent menu store
manually to the child menu store.

These properties are also included in the returned menu store object,
allowing you to verify whether the menu is nested. For instance:

    ```jsx
    const menu = useMenuStore(props);
    const isNested = Boolean(menu.parent);
    ```

- [`#2796`](#2796) Composed store
props such as `useSelectStore({ combobox })` now accept `null` as a
value.

## @ariakit/react@0.3.0

### Minor Changes

- [`#2714`](#2714) Added support
for a dynamic `store` prop on component stores.

This is similar to the `store` prop on components, keeping both stores
in sync. Now, component store hooks can support modifying the value of
the `store` prop after the initial render. For instance:

    ```js
    // props.store can change between renders now
    const checkbox = useCheckboxStore({ store: props.store });
    ```

When the `store` prop changes, the object returned from the store hook
will update as well. Consequently, effects and hooks that rely on the
store will re-run.

While it's unlikely, this **could represent a breaking change** if
you're depending on the `store` prop in component stores to only
acknowledge the first value passed to it.

- [`#2783`](#2783) **BREAKING**
_(This should affect very few people)_: The `combobox` state on
`useSelectStore` has been replaced by the `combobox` property on the
store object.

    **Before:**

    ```js
    const combobox = useComboboxStore();
    const select = useSelectStore({ combobox });
    const hasCombobox = select.useState("combobox");
    ```

    **After:**

    ```js
    const combobox = useComboboxStore();
    const select = useSelectStore({ combobox });
    const hasCombobox = Boolean(select.combobox);
    ```

In the example above, `select.combobox` is literally the same as the
`combobox` store. It will be defined if the `combobox` store is passed
to `useSelectStore`.

- [`#2783`](#2783) **BREAKING**
_(This should affect very few people)_: The `select` and `menu` props on
`useComboboxStore` have been removed. If you need to compose `Combobox`
with `Select` or `Menu`, use the `combobox` prop on `useSelectStore` or
`useMenuStore` instead.

- [`#2717`](#2717) The `children`
prop as a function has been deprecated on all components. Use the
[`render`](https://ariakit.org/guide/composition#explicit-render-function)
prop instead.

- [`#2717`](#2717) The `as` prop
has been deprecated on all components. Use the
[`render`](https://ariakit.org/guide/composition) prop instead.

- [`#2717`](#2717) The
`backdropProps` prop has been deprecated on `Dialog` and derived
components. Use the
[`backdrop`](https://ariakit.org/reference/dialog#backdrop) prop
instead.

- [`#2745`](#2745) Component
stores will now throw an error if they receive another store prop in
conjunction with default prop values.

### Patch Changes

- [`#2737`](#2737) Fixed
controlled component stores rendering with a stale state.

- [`#2783`](#2783) Component
store objects now contain properties for the composed stores passed to
them as props. For instance, `useSelectStore({ combobox })` will return
a `combobox` property if the `combobox` prop is specified.

- [`#2785`](#2785) Added `parent`
and `menubar` properties to the menu store. These properties are
automatically set when rendering nested menus or menus within a menubar.

Now, it also supports rendering nested menus that aren't nested in the
React tree. In this case, you would have to supply the parent menu store
manually to the child menu store.

These properties are also included in the returned menu store object,
allowing you to verify whether the menu is nested. For instance:

    ```jsx
    const menu = useMenuStore(props);
    const isNested = Boolean(menu.parent);
    ```

- [`#2795`](#2795) Updated the
`Menu` component so the `composite` and `typeahead` props are
automatically set to `false` when combining it with a `Combobox`
component.

This means you'll not need to explicitly pass `composite={false}` when
building a [Menu with
Combobox](https://ariakit.org/examples/menu-combobox) component.

- [`#2796`](#2796) Composed store
props such as `useSelectStore({ combobox })` now accept `null` as a
value.

-   Updated dependencies: `@ariakit/react-core@0.3.0`.

## @ariakit/react-core@0.3.0

### Minor Changes

- [`#2714`](#2714) Added support
for a dynamic `store` prop on component stores.

This is similar to the `store` prop on components, keeping both stores
in sync. Now, component store hooks can support modifying the value of
the `store` prop after the initial render. For instance:

    ```js
    // props.store can change between renders now
    const checkbox = useCheckboxStore({ store: props.store });
    ```

When the `store` prop changes, the object returned from the store hook
will update as well. Consequently, effects and hooks that rely on the
store will re-run.

While it's unlikely, this **could represent a breaking change** if
you're depending on the `store` prop in component stores to only
acknowledge the first value passed to it.

- [`#2714`](#2714) **BREAKING**:
The `useStore` function exported by `@ariakit/react-core/utils/store`
has been updated.

- [`#2760`](#2760) **BREAKING**:
The `useStoreState` function exported by
`@ariakit/react-core/utils/store` has been updated so it'll always run
the selector callback even when the passed store is `null` or
`undefined`.

- [`#2783`](#2783) **BREAKING**
_(This should affect very few people)_: The `combobox` state on
`useSelectStore` has been replaced by the `combobox` property on the
store object.

    **Before:**

    ```js
    const combobox = useComboboxStore();
    const select = useSelectStore({ combobox });
    const hasCombobox = select.useState("combobox");
    ```

    **After:**

    ```js
    const combobox = useComboboxStore();
    const select = useSelectStore({ combobox });
    const hasCombobox = Boolean(select.combobox);
    ```

In the example above, `select.combobox` is literally the same as the
`combobox` store. It will be defined if the `combobox` store is passed
to `useSelectStore`.

- [`#2783`](#2783) **BREAKING**
_(This should affect very few people)_: The `select` and `menu` props on
`useComboboxStore` have been removed. If you need to compose `Combobox`
with `Select` or `Menu`, use the `combobox` prop on `useSelectStore` or
`useMenuStore` instead.

- [`#2717`](#2717) The `children`
prop as a function has been deprecated on all components. Use the
[`render`](https://ariakit.org/guide/composition#explicit-render-function)
prop instead.

- [`#2717`](#2717) The `as` prop
has been deprecated on all components. Use the
[`render`](https://ariakit.org/guide/composition) prop instead.

- [`#2717`](#2717) The
`backdropProps` prop has been deprecated on `Dialog` and derived
components. Use the
[`backdrop`](https://ariakit.org/reference/dialog#backdrop) prop
instead.

- [`#2745`](#2745) Component
stores will now throw an error if they receive another store prop in
conjunction with default prop values.

### Patch Changes

- [`#2737`](#2737) Fixed
controlled component stores rendering with a stale state.

- [`#2758`](#2758) Added
`CheckboxProvider` component and `useCheckboxContext` hook.

- [`#2759`](#2759) Added
`CollectionProvider` component and `useCollectionContext` hook.

- [`#2769`](#2769) Added
`DisclosureProvider` component and `useDisclosureContext` hook.

- [`#2770`](#2770) Added
`CompositeProvider` component and `useCompositeContext` hook.

- [`#2771`](#2771) Added
`DialogProvider` component and `useDialogContext` hook.

- [`#2774`](#2774) Added
`PopoverProvider` component and `usePopoverContext` hook.

- [`#2775`](#2775) Added
`ComboboxProvider` component and `useComboboxContext` hook.

- [`#2776`](#2776) Added
`SelectProvider` component and `useSelectContext` hook.

- [`#2777`](#2777) Added
`RadioProvider` component and `useRadioContext` hook.

- [`#2778`](#2778) Added
`HovercardProvider` component and `useHovercardContext` hook.

- [`#2779`](#2779) Added
`TabProvider` component and `useTabContext` hook.

- [`#2780`](#2780) Added
`ToolbarProvider` component and `useToolbarContext` hook.

- [`#2781`](#2781) Added
`TooltipProvider` component and `useTooltipContext` hook.

- [`#2782`](#2782) Added
`FormProvider` component and `useFormContext` hook.

- [`#2783`](#2783) Component
store objects now contain properties for the composed stores passed to
them as props. For instance, `useSelectStore({ combobox })` will return
a `combobox` property if the `combobox` prop is specified.

- [`#2785`](#2785) Added
`MenuProvider` component and `useMenuContext` hook.

- [`#2785`](#2785) Added
`MenuBarProvider` component and `useMenuBarContext` hook.

- [`#2785`](#2785) Added `parent`
and `menubar` properties to the menu store. These properties are
automatically set when rendering nested menus or menus within a menubar.

Now, it also supports rendering nested menus that aren't nested in the
React tree. In this case, you would have to supply the parent menu store
manually to the child menu store.

These properties are also included in the returned menu store object,
allowing you to verify whether the menu is nested. For instance:

    ```jsx
    const menu = useMenuStore(props);
    const isNested = Boolean(menu.parent);
    ```

- [`#2794`](#2794) The `combobox`
prop on `useSelectStore` is now automatically set based on the context.

- [`#2795`](#2795) Updated the
`Menu` component so the `composite` and `typeahead` props are
automatically set to `false` when combining it with a `Combobox`
component.

This means you'll not need to explicitly pass `composite={false}` when
building a [Menu with
Combobox](https://ariakit.org/examples/menu-combobox) component.

- [`#2795`](#2795) The `combobox`
prop on `useMenuStore` is now automatically set based on the context.

- [`#2796`](#2796) Composed store
props such as `useSelectStore({ combobox })` now accept `null` as a
value.

-   Updated dependencies: `@ariakit/core@0.3.0`.

## @ariakit/test@0.2.3

### Patch Changes

-   Updated dependencies: `@ariakit/core@0.3.0`.

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants