Created
May 28, 2020 19:25
-
-
Save fmoliveira/77513eb0980c163c994d6f4ab83f0f92 to your computer and use it in GitHub Desktop.
Storybook Decorator for Auto Focus
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useCallback } from 'react'; | |
/** | |
* Storybook Decorator | |
* | |
* Auto click the first focusable element. | |
*/ | |
const AutoFocusStory = (storyFn: () => {} | null | undefined) => { | |
const autofocus = useCallback((node: HTMLDivElement) => { | |
const element = node.querySelector('button, [href], input, select, textarea, [tabindex]:not([tabindex="-1"])'); | |
if (element instanceof HTMLElement) { | |
element.click(); | |
} | |
}, []); | |
return ( | |
<div ref={autofocus}> | |
{storyFn()} | |
</div> | |
); | |
}; | |
export default AutoFocusStory; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment