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

[Experiment] feat(react-hooks): add react-hooks #3896

Merged

Conversation

joshblack
Copy link
Contributor

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 our instanceId helpers, provides a unique id for the lifecycle of a component
  • usePortalNode: is a way to get access to a node in which one could render a portal, used in useAnnouncer to get access to a singleton #carbon-announcer node
  • useAnnouncer: a way to control displaying polite and assertive messages in dedicated aria-live regions

In 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

  • Each of the hooks has an associated story and test suite to check out
  • useAnnouncer is an interesting use case, here's a video for a full overview of what to expect when testing with VO

demo

@joshblack joshblack requested a review from a team September 3, 2019 16:25
@ghost ghost requested review from asudoh and removed request for a team September 3, 2019 16:25
@netlify
Copy link

netlify bot commented Sep 3, 2019

Deploy preview for the-carbon-components ready!

Built with commit 6c0f2f2

https://deploy-preview-3896--the-carbon-components.netlify.com

@netlify
Copy link

netlify bot commented Sep 3, 2019

Deploy preview for carbon-components-react ready!

Built with commit 6c0f2f2

https://deploy-preview-3896--carbon-components-react.netlify.com

@netlify
Copy link

netlify bot commented Sep 3, 2019

Deploy preview for carbon-elements ready!

Built with commit 6c0f2f2

https://deploy-preview-3896--carbon-elements.netlify.com

* messages sent is important.
*/
export function useAnnouncer() {
const node = usePortalNode('carbon-announcer', node => {
Copy link
Contributor Author

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 🤔

Copy link
Contributor

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 😸

Copy link
Contributor

@dakahn dakahn left a 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 => {
Copy link
Contributor

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) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This is very readable 👍

@joshblack joshblack merged commit 87e99d6 into carbon-design-system:master Sep 4, 2019
@joshblack joshblack deleted the feat/add-react-hooks-package branch September 4, 2019 21:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants