UNPKG

@primer/react

Version:

An implementation of GitHub's Primer Design System using React

44 lines (41 loc) 1.51 kB
import { clsx } from 'clsx'; import React from 'react'; import { useDevOnlyEffect } from '../internal/hooks/useDevOnlyEffect.js'; import classes from './Link.module.css.js'; import { fixedForwardRef } from '../utils/modern-polymorphic.js'; import { jsx } from 'react/jsx-runtime'; import { useMergedRefs } from '../hooks/useMergedRefs.js'; const UnwrappedLink = (props, ref) => { const { as: Component = 'a', className, inline, muted, hoverColor, ...restProps } = props; const innerRef = React.useRef(null); const mergedRef = useMergedRefs(ref, innerRef); useDevOnlyEffect(() => { if (innerRef.current && !(innerRef.current instanceof HTMLButtonElement) && !(innerRef.current instanceof HTMLAnchorElement)) { // eslint-disable-next-line no-console console.error('Error: Found `Link` component that renders an inaccessible element', innerRef.current, 'Please ensure `Link` always renders as <a> or <button>'); } }, [innerRef]); return /*#__PURE__*/jsx(Component, { className: clsx(className, classes.Link), "data-component": "Link", "data-muted": muted, "data-inline": inline, "data-hover-color": hoverColor, ...restProps, // eslint-disable-next-line @typescript-eslint/no-explicit-any ref: mergedRef }); }; UnwrappedLink.displayName = "UnwrappedLink"; const LinkComponent = fixedForwardRef(UnwrappedLink); const Link = Object.assign(LinkComponent, { displayName: 'Link' }); export { UnwrappedLink, Link as default };