Provide `ref` function to access the menu?
See original GitHub issueContext: I would like to blur the menu when a selection is made, because the design for the focused state of our menu looks very “active”.
Is it possible to provide a ref
function to access the menu’s focusable DOM element? I’m not sure if this would be tricky given the implementation of the library or not. Ideally I would be able to just call .blur()
(or .focus()
, etc.) on the menu whenever I’d like using a Ref.
Right now, I am doing this by:
import * as M from 'react-aria-menubutton'
...
<M.Wrapper
{...wrapperProps}
onSelection={(selected: KeyType) => {
onSelection(selected)
// Blur the menu after selecting. we don't have a way to
// get a ref for the menu, so this will have to do
if (blurOnSelection) {
// @ts-ignore
document.activeElement &&
// @ts-ignore
document.activeElement.blur &&
// @ts-ignore
typeof document.activeElement.blur === 'function' &&
// @ts-ignore
document.activeElement.blur()
}
}}
>
Issue Analytics
- State:
- Created 5 years ago
- Comments:5 (5 by maintainers)
Top Results From Across the Web
How to use custom functional components within Material-UI ...
In order to leverage the first child as the content anchor, Menu adds a ref to it (using cloneElement). In order to not...
Read more >Menu Component "Function components cannot be given refs"
Attempts to access this ref will fail. Did you mean to use React.forwardRef()?. Check the render method of ForwardRef(Menu) .
Read more >ref - dbt Developer Hub
The {{ ref }} function returns a Relation object that has the same table , schema , and name attributes at the {{...
Read more >Everything You Need to Know About Refs in React
Short for “reference”, refs are a way to access underlying DOM elements in a React component. There are many reasons why you would...
Read more >How to use React's forwardRef function - Felix Gerschau
forwardRef is a helper function from React that allows us to forward a component's ref to another one. This tutorial will teach what...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
This seems like it can be addressed by separating the active and focused state in the design instead of exposing the ref escape hatch. Additionally, the examples provided on w3 all keep the focus state on selection.
It’s possible it’s not ARIA-favorable behavior to unfocus the menu on selection, but I think generally speaking it would be valuable to expose a
ref
from this library for focus state management.