-
-
Notifications
You must be signed in to change notification settings - Fork 367
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
chore: Add collection
example
#977
Conversation
This pull request is being automatically deployed with Vercel (learn more). ariakit – ./🔍 Inspect: https://vercel.com/ariakit/ariakit/8eHUtutcqAhH3XZMQttdLi7yXNRD reakit – ./🔍 Inspect: https://vercel.com/ariakit/reakit/J8Jz17jyM2K16s5CjtxKR2wGgVw3 [Deployment for a379ef1 canceled] |
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. |
Codecov Report
@@ Coverage Diff @@
## v2 #977 +/- ##
==========================================
+ Coverage 64.25% 64.58% +0.33%
==========================================
Files 170 171 +1
Lines 4233 4236 +3
Branches 1167 1167
==========================================
+ Hits 2720 2736 +16
+ Misses 1512 1499 -13
Partials 1 1
Continue to review full report at Codecov.
|
@diegohaz let me know if the example is in line with the visual styles and the contents that have been used throughout the project. For the test, I couldn't really think of anything in particular to test for, since the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you @ciampo!
The main use for the Collection
components is to keep the state in sync with a collection of DOM elements. For example, given the following elements:
const collection = useCollectionState();
<Collection state={collection}>
<CollectionItem>Apple</CollectionItem>
<CollectionItem>Grape</CollectionItem>
<CollectionItem>Orange</CollectionItem>
</Collection>
I should be able to access the elements refs through the collection state:
collection.items.forEach((item) => {
console.log(item.ref.current);
});
But it's really useful in combination with the getItem
prop, which allows us to register any prop to the state besides the element ref:
function ToolsPanelItem({ value, isDisabled, ...props }) {
const getItem = React.useCallback(
(defaultItem) => ({ ...defaultItem, value, isDisabled }),
[value, isDisabled]
);
return <CollectionItem getItem={getItem} hidden={isDisabled} {...props} />
}
function Panel() {
const collection = useCollectionState();
<Collection state={collection}>
<ToolsPanelItem value="Apple">Apple</ToolsPanelItem>
<ToolsPanelItem value="Grape" isDisabled>Grape</ToolsPanelItem>
<ToolsPanelItem value="Orange">Orange</ToolsPanelItem>
</Collection>
}
We can now access value
and isDisabled
through the state:
collection.items.map((item) => (
<MenuItemCheckbox
key={item.value}
checked={!isDisabled}
value={item.value}
>
{item.value}
</MenuItemCheckbox>
));
It also keeps the order of the elements in sync with the DOM, so rendering elements dynamically will not push them to the end of the items list in the state:
<CollectionItem>Apple</CollectionItem>
{isAdmin && <CollectionItem>Grape</CollectionItem>}
<CollectionItem>Orange</CollectionItem>
In the example above, Grape
will be the second element even if it's lazily rendered.
As a more concrete use case, this could be used here to keep the menu items in sync with the actual panel tools:
That being said, that panel is a really complex example, and I don't think we should have something like that as the default example. Even the getItem
prop should probably be another example. But maybe a simple way to exemplify the collection features is to render a count.
packages/ariakit/src/collection/__examples__/collection/index.tsx
Outdated
Show resolved
Hide resolved
packages/ariakit/src/collection/__examples__/collection/style.css
Outdated
Show resolved
Hide resolved
packages/ariakit/src/collection/__examples__/collection/style.css
Outdated
Show resolved
Hide resolved
packages/ariakit/src/collection/__examples__/collection/test.tsx
Outdated
Show resolved
Hide resolved
Collection
default examplecollection
example
Thank you @diegohaz for the review — I addressed all the feedback so far.
Yeah, that example is quite complex and I'm not sure I get exactly how, in your example,
All of this insight is great, and I think it would benefit the consumers of the component is it was added to its README. Right now the documentation for the component is not very clear about what this component is useful for and its main features. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome! Thank you!
Yeah! That was just something I made off the top of my head. But here's a quick prototype of how that could work (https://codesandbox.io/s/ariakit-toolspanel-nf8xf?file=/App.tsx). Basically,
Agree! We should definitely include an explanation in the docs. |
Part of #939
Adds the default example for the
Collection
component and a simple test