Skip to content

Commit eeaa8f4

Browse files
Improve atom HTML element A to handle innerRef forwarding
The currently used version of "Gatsby Link" (1) uses the `innerRef` prop to allow `ref` access to the underlying DOM element (necessary for animations). In order to wrap animated components the `ref` is necessary and is therefore now forwarded. The legacy behavior of "Gatsby Link" is about to change in the future to the `React.forwardRef` API. See gatsbyjs/gatsby#9892 for more details. References: (1) https://www.gatsbyjs.org/docs/gatsby-link Associated epic: GH-69 GH-64
1 parent 71a309c commit eeaa8f4

File tree

2 files changed

+36
-11
lines changed
  • src/components/atoms/core/HTMLElements/inlineTextSemantics
  • test/components/atoms/core/HTMLElements/inlineTextSemantics/__snapshots__

2 files changed

+36
-11
lines changed

src/components/atoms/core/HTMLElements/inlineTextSemantics/A.jsx

+32-7
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616
import React from "react";
1717
import PropTypes from "prop-types";
1818
import { Link } from "gatsby";
19-
import styled from "styled-components";
19+
import styled, { css } from "styled-components";
2020

2121
import { isRouteInternal } from "utils";
2222

23-
const BaseComponent = styled.a`
23+
const baseStyles = css`
2424
color: inherit;
2525
cursor: pointer;
2626
text-decoration: none;
@@ -33,28 +33,49 @@ const BaseComponent = styled.a`
3333
}
3434
`;
3535

36+
const BaseComponent = styled.a`
37+
${baseStyles};
38+
`;
39+
40+
const BaseGatsbyLink = styled(Link)`
41+
${baseStyles}
42+
`;
43+
3644
/**
3745
* A dynamic and failsafe component which either renders to a base HTML link `<a>` (anchor) or a "Gatsby Link" based on
3846
* the target/URL type, internal or external, passed to the `to` and `href` props.
3947
*
48+
* Note: The currently used version of "Gatsby Link" uses the `innerRef` prop to allow `ref` access to the underlying
49+
* DOM element (necessary for animations). This legacy behavior is about to change in the future to the
50+
* `React.forwardRef` API.
51+
* See https://github.com/gatsbyjs/gatsby/pull/9892 for more details.
52+
*
4053
* @example <caption>Usage</caption>
4154
* <!-- The target is external so both will render to `<a>` with the `href` prop. -->
4255
* <A href="https://arcticicestudio.github.io/nord">Nord</A>
4356
* <A to="https://arcticicestudio.github.io/nord">Nord</A>
4457
* <!-- The target is internal so both will render to `<Link>` with the `to` prop. -->
4558
* <A to="/blog">Blog</A>
4659
* <A href="/blog">Blog</A>
60+
* <!-- Allow ref access to the underlying "Gatsby Link" DOM element. -->
61+
* const RefLink = React.forwardRef(({children, ...passProps}, ref) => (
62+
* <A innerRef={ref} {...passProps}>
63+
* {children}
64+
* </A>
65+
* );
66+
* <RefLink to="/blog">Blog</RefLink>
4767
* @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a
4868
* @see https://www.gatsbyjs.org/docs/gatsby-link
69+
* @see https://github.com/gatsbyjs/gatsby/pull/9892
4970
* @since 0.3.0
5071
*/
51-
const A = ({ children, href, to, ...passProps }) =>
72+
const A = ({ children, href, to, linkRef, ...passProps }) =>
5273
isRouteInternal(to) || isRouteInternal(href) ? (
53-
<BaseComponent as={Link} to={to || href} {...passProps}>
74+
<BaseGatsbyLink innerRef={linkRef} to={to || href} {...passProps}>
5475
{children}
55-
</BaseComponent>
76+
</BaseGatsbyLink>
5677
) : (
57-
<BaseComponent href={href || to} {...passProps}>
78+
<BaseComponent ref={linkRef} href={href || to} {...passProps}>
5879
{children}
5980
</BaseComponent>
6081
);
@@ -66,4 +87,8 @@ A.propTypes = {
6687

6788
A.defaultProps = { to: "" };
6889

69-
export default A;
90+
export default React.forwardRef(({ children, ...passProps }, ref) => (
91+
<A linkRef={ref} {...passProps}>
92+
{children}
93+
</A>
94+
));

test/components/atoms/core/HTMLElements/inlineTextSemantics/__snapshots__/A.test.jsx.snap

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
exports[`snapshot renders external URLs with \`href\` prop 1`] = `
44
<div>
55
<a
6-
class="sc-bdVaJa ccNLbx"
6+
class="sc-bdVaJa jEake"
77
href="https://nordtheme.com"
88
>
99
Docs
@@ -14,7 +14,7 @@ exports[`snapshot renders external URLs with \`href\` prop 1`] = `
1414
exports[`snapshot renders external URLs with \`to\` prop 1`] = `
1515
<div>
1616
<a
17-
class="sc-bdVaJa ccNLbx"
17+
class="sc-bdVaJa jEake"
1818
href="https://nordtheme.com"
1919
>
2020
Docs
@@ -25,7 +25,7 @@ exports[`snapshot renders external URLs with \`to\` prop 1`] = `
2525
exports[`snapshot renders inernal URLs with \`href\` prop 1`] = `
2626
<div>
2727
<a
28-
class="sc-bdVaJa ccNLbx"
28+
class="sc-bwzfXH lhDJTy"
2929
href="/docs"
3030
>
3131
Docs
@@ -36,7 +36,7 @@ exports[`snapshot renders inernal URLs with \`href\` prop 1`] = `
3636
exports[`snapshot renders inernal URLs with \`to\` prop 1`] = `
3737
<div>
3838
<a
39-
class="sc-bdVaJa ccNLbx"
39+
class="sc-bwzfXH lhDJTy"
4040
href="/docs"
4141
>
4242
Docs

0 commit comments

Comments
 (0)