Skip to content

Instantly share code, notes, and snippets.

@iMerica
Created January 30, 2020 23:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iMerica/a30a2451196aa499d6435cc5f49190ea to your computer and use it in GitHub Desktop.
Save iMerica/a30a2451196aa499d6435cc5f49190ea to your computer and use it in GitHub Desktop.
Fixes Anchor Focus Issue
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