-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
[Experiment] feat(react-hooks): add react-hooks #3896
[Experiment] feat(react-hooks): add react-hooks #3896
Conversation
…feat/add-react-hooks-package
Deploy preview for the-carbon-components ready! Built with commit 6c0f2f2 https://deploy-preview-3896--the-carbon-components.netlify.com |
Deploy preview for carbon-components-react ready! Built with commit 6c0f2f2 https://deploy-preview-3896--carbon-components-react.netlify.com |
Deploy preview for carbon-elements ready! Built with commit 6c0f2f2 |
…arbon into feat/add-react-hooks-package
* messages sent is important. | ||
*/ | ||
export function useAnnouncer() { | ||
const node = usePortalNode('carbon-announcer', node => { |
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.
I'm curious if we want a callback to usePortalNode
, or if we should just put this in an effect 🤔
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.
this is pretty clear to read. I like it 😸
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.
This seems great! Should we get a ticket going that tracks some usage documentation around -- i'm thinking in particular the announcer, but the other hooks as well. I'm also wondering if we should get something on paper about what warrants making something a hook.
* messages sent is important. | ||
*/ | ||
export function useAnnouncer() { | ||
const node = usePortalNode('carbon-announcer', node => { |
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.
this is pretty clear to read. I like it 😸
const assertiveNode = node.querySelector('#carbon-assertive-announcement'); | ||
const politeNode = node.querySelector('#carbon-polite-announcement'); | ||
const timeoutId = setTimeout(() => { | ||
if (mode === 'assertive' && assertiveNode.textContent !== announcement) { |
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.
This is very readable 👍
This is an exploratory PR to gauge interest in a dedicated
react-hooks
package that could surface helpful hooks not only for our own project, but to be exposed for others to consume.In this PR, we introduce three hooks:
useId
: equivalent to ourinstanceId
helpers, provides a unique id for the lifecycle of a componentusePortalNode
: is a way to get access to a node in which one could render a portal, used inuseAnnouncer
to get access to a singleton#carbon-announcer
nodeuseAnnouncer
: a way to control displaying polite and assertive messages in dedicated aria-live regionsIn the future, it would be awesome to consolidate hooks we use across projects to make it easier for product teams to use/leverage them. I would love to expand this package to add support for window events, in particular for things like scroll/resize/scroll direction
Changelog
New
packages/react-hooks
useId
usePortalNode
useAnnouncer
Changed
Removed
Testing / Reviewing
useAnnouncer
is an interesting use case, here's a video for a full overview of what to expect when testing with VO