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

[useRenderer] Add public hook #1418

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open

Conversation

mnajdova
Copy link
Member

@mnajdova mnajdova commented Feb 5, 2025

Closes #1315

Added a public useRenderer hook as an simpler adapter that uses the useComponentRenderer hook. I intentionally copied the types, so we make sure we don't break them if we ever change the internal useComponentRenderer hook.

The hooks receives the following settings:

  • className
  • render
  • state
  • ref
  • props (passed as extraProps)
  • styleHookMapping

Documentation page: https://deploy-preview-1418--base-ui.netlify.app/react/utils/use-renderer

Copy link

netlify bot commented Feb 5, 2025

Deploy Preview for base-ui ready!

Name Link
🔨 Latest commit 2a9621f
🔍 Latest deploy log https://app.netlify.com/sites/base-ui/deploys/67a62d26db69110008b6cf5b
😎 Deploy Preview https://deploy-preview-1418--base-ui.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@mj12albert mj12albert linked an issue Feb 5, 2025 that may be closed by this pull request
@mnajdova mnajdova marked this pull request as draft February 5, 2025 11:56
@mnajdova mnajdova added the new feature New feature or request label Feb 5, 2025
Copy link
Member

@mj12albert mj12albert left a 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

@mnajdova
Copy link
Member Author

mnajdova commented Feb 5, 2025

for some reason importing the hook in CSB doesn't work, this works fine locally in a playground though

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

@mj12albert
Copy link
Member

I removed only the customStyleHookMapping setting from the original hook

It may be useful to provide a (alternative) way to opt-out individual props placed in state from generating a corresponding data attribute

@mnajdova
Copy link
Member Author

mnajdova commented Feb 6, 2025

Thanks for the initial review, I updated the API to include:

@michaldudak
Copy link
Member

I'd leave customStyleHookMapping as in useComponentRenderer. It should be quite common to customize the data attributes (we do this often).

@mnajdova
Copy link
Member Author

mnajdova commented Feb 6, 2025

I'd leave customStyleHookMapping as in useComponentRenderer. It should be quite common to customize the data attributes (we do this often).

Yeah, fair enough, ok I was thinking initially to make the API simpler. I've changed it back to use the customStyleHookMapping and added a test for it.

@mnajdova mnajdova marked this pull request as ready for review February 7, 2025 08:45
@mnajdova
Copy link
Member Author

mnajdova commented Feb 7, 2025

I've resolved all feedback and added a docs page: https://deploy-preview-1418--base-ui.netlify.app/react/utils/use-renderer

mnajdova and others added 2 commits February 7, 2025 10:23
Co-authored-by: Aarón García Hervás <[email protected]>
Signed-off-by: Marija Najdova <[email protected]>
@mnajdova mnajdova requested a review from michaldudak February 7, 2025 09:33
Comment on lines 61 to 65
styleHookMapping: {
excludedProp() {
return null;
},
},
Copy link
Contributor

@atomiks atomiks Feb 7, 2025

Choose a reason for hiding this comment

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

I'm not the biggest fan of this prop name (styleHookMapping) - not sure if "style hook" is suitable for a public API, and Map instead of Mapping might be better. It's not clear it infers/uses the state object

Copy link
Member Author

Choose a reason for hiding this comment

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

The most clear name would be: stateToDataAttributesMap, to be very explicit, but it's a strange name 😅 @aarongarciah or @colmtuite do you have some suggestions?

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, "hook" has a strong different meaning in React world, so we can think of something else. stateAttributes?

Copy link
Member

Choose a reason for hiding this comment

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

stateDataAttributes?

Copy link
Member

Choose a reason for hiding this comment

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

Technically, you could create any attribute (including class), so "dataAttributes" is not entirely correct. We just happen to use only data attributes.

Copy link
Member

Choose a reason for hiding this comment

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

I see! stateAttributes makes sense then.

Copy link
Member Author

Choose a reason for hiding this comment

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

Renamed to stateDataAttributes, I am still missing the mapping in it, but people can figure out how to define them by using the type. Maybe it's even worth adding demo about it on the page. What do you think?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new feature New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Slot utility [core] Could useComponentRenderer be exported as a util?
5 participants