Skip to content

Commit

Permalink
[react-web] Forward ref in PlasmicLink
Browse files Browse the repository at this point in the history
GitHub issue: #27

Change-Id: I2cf589d26bf702cafd79f34b2ba565a7beab64db
  • Loading branch information
tmadeira committed Jan 21, 2022
1 parent 3e0c038 commit 19213b1
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
2 changes: 1 addition & 1 deletion packages/react-web/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@plasmicapp/react-web",
"version": "0.2.90",
"version": "0.2.91",
"description": "plasmic library for rendering in the presentational style",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
13 changes: 8 additions & 5 deletions packages/react-web/src/render/PlasmicLink.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import React from "react";
import { omit, pick } from "../common";

export function PlasmicLink(props: any) {
export const PlasmicLink = React.forwardRef(function PlasmicLink(
props: any,
ref: React.Ref<any>
) {
// props.href is required for nextjs; if no props.href,
// then we just render the default anchor element
if (props.platform === "nextjs" && props.href) {
Expand All @@ -18,19 +21,19 @@ export function PlasmicLink(props: any) {
return React.createElement(
props.component,
pick(props, ...nextjsProps),
<a {...omit(props, "component", "platform", ...nextjsProps)} />
<a {...omit(props, "component", "platform", ...nextjsProps)} ref={ref} />
);
}

if (props.platform === "gatsby" && isInternalHref(props.href)) {
return React.createElement(props.component, {
...omit(props, "component", "platform", "href"),
...{ to: props.href },
...{ to: props.href, ref },
});
}

return <a {...omit(props, "component", "platform")} />;
}
return <a {...omit(props, "component", "platform")} ref={ref} />;
});

function isInternalHref(href: string): boolean {
return /^\/(?!\/)/.test(href);
Expand Down

0 comments on commit 19213b1

Please sign in to comment.