UNPKG

1.84 kBJavaScriptView Raw
1import _objectSpread from '@babel/runtime/helpers/esm/objectSpread2';
2import { Box } from '@spark-web/box';
3import { forwardRefWithAs } from '@spark-web/utils/ts';
4import _taggedTemplateLiteral from '@babel/runtime/helpers/esm/taggedTemplateLiteral';
5import dedent from 'dedent';
6import { assert } from 'emery';
7import { forwardRef, createContext, useContext } from 'react';
8import { jsx } from 'react/jsx-runtime';
9
10var _templateObject;
11var makeLinkComponent = function makeLinkComponent(render) {
12 return {
13 __forwardRef__: /*#__PURE__*/forwardRef(render)
14 };
15};
16var DefaultLinkComponent = makeLinkComponent(function (props, ref) {
17 return /*#__PURE__*/jsx("a", _objectSpread({
18 ref: ref
19 }, props));
20});
21var LinkComponentContext = /*#__PURE__*/createContext(DefaultLinkComponent);
22var useLinkComponent = function useLinkComponent(ref) {
23 var linkComponent = useContext(LinkComponentContext);
24 assert(!ref || '__forwardRef__' in linkComponent, dedent(_templateObject || (_templateObject = _taggedTemplateLiteral(["\n You're passing a ref to a Spark link, but your app is providing a custom link component to 'SparkProvider' that doesn't appear to support refs.\n\n To fix this, you need to use Spark's 'makeLinkComponent' helper function when creating your custom link component. This ensures that refs are forwarded correctly, and allows us to silence this error message.\n "]))));
25 if ('__forwardRef__' in linkComponent) {
26 return linkComponent.__forwardRef__;
27 }
28 return linkComponent;
29};
30
31var Link = forwardRefWithAs(function (props, ref) {
32 var LinkComponent = useLinkComponent(ref);
33 return /*#__PURE__*/jsx(Box, _objectSpread({
34 as: LinkComponent,
35 asElement: "a",
36 ref: ref
37 }, props));
38});
39
40export { DefaultLinkComponent, Link, LinkComponentContext, makeLinkComponent, useLinkComponent };