curls
Version:
💪 Responsive, expressive UI primitives for React written with Style Hooks and Emotion
66 lines (60 loc) • 1.78 kB
JavaScript
function _extends() {
_extends =
Object.assign ||
function(target) {
for (var i = 1; i < arguments.length; i++) {
var source = arguments[i]
for (var key in source) {
if (Object.prototype.hasOwnProperty.call(source, key)) {
target[key] = source[key]
}
}
}
return target
}
return _extends.apply(this, arguments)
}
import React, {useRef} from 'react'
import useLayoutEffect from '@react-hook/passive-layout-effect'
import useMergedRef from '@react-hook/merged-ref'
import {createElement} from '@style-hooks/core'
import {createStyleHook} from '@style-hooks/core'
import {Button} from './Button'
export const useAriaPopupToggle = (props, context) => {
const focusRef = useRef(null)
const seen = useRef(false)
useLayoutEffect(() => {
if (context.isOpen === false) {
if (seen.current === true) focusRef.current.focus()
seen.current = true
}
}, [context.isOpen])
const nextProps = _extends(
{
tabIndex: 0,
'aria-controls': context.id,
'aria-haspopup': 'true',
'aria-expanded': String(context.isOpen),
ref: focusRef,
},
props
)
nextProps.onClick = () => {
var _props$onClick
props === null || props === void 0
? void 0
: (_props$onClick = props.onClick) === null || _props$onClick === void 0
? void 0
: _props$onClick.call(props)
context.toggle()
}
return nextProps
}
export default (name, useContext) => {
const useStylesHook = createStyleHook(name, {})
return React.forwardRef((props, ref) => {
const nextProps = useStylesHook(useAriaPopupToggle(props, useContext()))
nextProps.ref = useMergedRef(ref, nextProps.ref)
return createElement(Button, nextProps)
})
}