-
-
Notifications
You must be signed in to change notification settings - Fork 94
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
[useRender] Add public hook #1418
base: master
Are you sure you want to change the base?
Conversation
✅ Deploy Preview for base-ui ready!
To edit notification comments on pull requests, go to your Netlify site configuration. |
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.
Nice ~ I imagine the use-case is something like this: https://codesandbox.io/p/sandbox/flamboyant-fog-9x266p?file=%2Fsrc%2FApp.tsx%3A29%2C63
(for some reason importing the hook in CSB doesn't work, this works fine locally in a playground though)
This feels more intuitive to me (biased though because I never used asChild extensively) to use than a Slot component
I haven't updated the exports field, will fix it. Here is the updated sandbox: https://codesandbox.io/p/sandbox/exciting-paper-9x266p-use-renderer-forked-ry269y?file=%2Fpackage.json |
It may be useful to provide a (alternative) way to opt-out individual props placed in state from generating a corresponding data attribute |
Thanks for the initial review, I updated the API to include:
|
I'd leave |
Yeah, fair enough, ok I was thinking initially to make the API simpler. I've changed it back to use the |
I've resolved all feedback and added a docs page: https://deploy-preview-1418--base-ui.netlify.app/react/utils/use-renderer |
docs/src/app/(public)/(content)/react/utils/use-renderer/page.mdx
Outdated
Show resolved
Hide resolved
docs/src/app/(public)/(content)/react/utils/use-renderer/page.mdx
Outdated
Show resolved
Hide resolved
docs/src/app/(public)/(content)/react/utils/use-renderer/page.mdx
Outdated
Show resolved
Hide resolved
docs/src/app/(public)/(content)/react/utils/use-renderer/page.mdx
Outdated
Show resolved
Hide resolved
docs/src/app/(public)/(content)/react/utils/use-renderer/demos/usage/css-modules/index.tsx
Outdated
Show resolved
Hide resolved
a654569
to
631d1d2
Compare
...rc/app/(public)/(content)/react/utils/use-render/demos/data-attributes/css-modules/index.tsx
Outdated
Show resolved
Hide resolved
@@ -26,4 +26,5 @@ export * from './tabs'; | |||
export * from './toggle'; | |||
export * from './toggle-group'; | |||
export * from './tooltip'; | |||
export * from './use-render'; |
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.
Why useRender
is not inside /utils
?
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.
@michaldudak @atomiks what are your thoughts? I though it as a "first class export" similar to the components, but having it in the utils makes sense as well, especially if we want to publicly make available more of the hooks.
Docs updated. Highlights:
I'm still not happy with the subtitle ("Utility for adding Base UI-like features to custom components") and the intro bullet points below. I think 99% os users will use One of the reasons why it's so hard to come up with simple examples for the data-attrs and className callback demos is because it's mostly useful for unstyled primitives, and unstyled primitives already exist on Base UI, and tend to be complex (or at least require a lot of boilerplate to be fully accessible). Note: state-based data attributes can be useful for styled components that want to have self-contained styles based on data attributes ( After having though about |
eaaa553
to
2ed9a60
Compare
...rc/app/(public)/(content)/react/utils/use-render/demos/data-attributes/css-modules/index.tsx
Outdated
Show resolved
Hide resolved
...(public)/(content)/react/utils/use-render/demos/data-attributes/css-modules/index.module.css
Outdated
Show resolved
Hide resolved
703328e
to
ab39665
Compare
How about "Utility for creating custom components that support a render prop"? |
It's not a bad idea, especially considering that state attributes and class names implementation is pretty simple and should be easily done in userland if needed (or we could provide separate utilities for them if devs want them). |
'The state of the component. It will be used as a parameter for the render and className callbacks.', | ||
}, | ||
stateAttributesMap: { | ||
type: 'StateAttributes<State>', |
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.
AFAIK StateAttributes
is not exposed to users so not sure if having it here is helpful.
Yeah, we can do this in the first iteration. @aarongarciah I will do the change on the hook & docs, if we decide to extend it in the future, we can use your examples. |
I've simplified the hook & the docs page to only cover the render prop, we can add more utils in the future if people ask for them. |
docs/src/app/(public)/(content)/react/utils/use-render/page.mdx
Outdated
Show resolved
Hide resolved
docs/src/app/(public)/(content)/react/utils/use-render/page.mdx
Outdated
Show resolved
Hide resolved
docs/src/app/(public)/(content)/react/utils/use-render/page.mdx
Outdated
Show resolved
Hide resolved
|
||
return useComponentRenderer({ | ||
render, | ||
state: {} as Record<string, any>, |
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.
off-topic: I wonder if state
should be optional in useComponentRenderer
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.
We probably didn't have use-case for it so far, as all components have some state, but we could consider it if we hit this need again.
Co-authored-by: Aarón García Hervás <[email protected]> Signed-off-by: Marija Najdova <[email protected]>
Co-authored-by: Aarón García Hervás <[email protected]> Signed-off-by: Marija Najdova <[email protected]>
Co-authored-by: Aarón García Hervás <[email protected]> Signed-off-by: Marija Najdova <[email protected]>
Co-authored-by: Aarón García Hervás <[email protected]> Signed-off-by: Marija Najdova <[email protected]>
Closes #1315
Added a public
useRender
hook as an simpler adapter that uses theuseComponentRenderer
hook. I intentionally copied the types, so we make sure we don't break them if we ever change the internaluseComponentRenderer
hook.The hooks receives the following settings:
customStyleHookMapping
)Documentation page: https://deploy-preview-1418--base-ui.netlify.app/react/utils/use-render