Created
January 30, 2020 23:54
-
-
Save iMerica/a30a2451196aa499d6435cc5f49190ea to your computer and use it in GitHub Desktop.
Fixes Anchor Focus Issue
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, { forwardRef, useState, cloneElement, useContext, useEffect } from 'react' | |
import { normalizeColor } from "grommet/utils/colors" | |
import { Box, ThemeContext } from "grommet" | |
import { StyledAnchor } from "grommet/components/Anchor/StyledAnchor" | |
export const Anchor = forwardRef( | |
( | |
{ | |
a11yTitle, | |
children, | |
color, | |
disabled, | |
href, | |
icon, | |
label, | |
onBlur, | |
onClick, | |
onFocus, | |
reverse, | |
...rest | |
}, | |
ref, | |
) => { | |
const theme = useContext(ThemeContext) || defaultProps.theme; | |
const [focus, setFocus] = useState(); | |
useEffect(() => { | |
if ((icon || label) && children) { | |
console.warn( | |
'Anchor should not have children if icon or label is provided', | |
); | |
} | |
}, [children, icon, label]); | |
let coloredIcon = icon; | |
if (icon && !icon.props.color) { | |
coloredIcon = cloneElement(icon, { | |
color: normalizeColor(color || theme.anchor.color, theme), | |
}); | |
} | |
const first = reverse ? label : coloredIcon; | |
const second = reverse ? coloredIcon : label; | |
return ( | |
<StyledAnchor | |
{...rest} | |
ref={ref} | |
aria-label={a11yTitle} | |
colorProp={color} | |
disabled={disabled} | |
hasIcon={!!icon} | |
hasLabel={label} | |
reverse={reverse} | |
href={!disabled ? href : undefined} | |
onClick={!disabled ? onClick : undefined}> | |
{first && second ? ( | |
<Box | |
as="span" | |
direction="row" | |
align="center" | |
gap="small" | |
style={{ display: 'inline-flex' }} | |
> | |
{first} | |
{second} | |
</Box> | |
) : ( | |
first || second || children | |
)} | |
</StyledAnchor> | |
); | |
}, | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment