UNPKG

3.48 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, '__esModule', { value: true });
4
5var _objectSpread = require('@babel/runtime/helpers/objectSpread2');
6var _objectWithoutProperties = require('@babel/runtime/helpers/objectWithoutProperties');
7var css = require('@emotion/css');
8var box = require('@spark-web/box');
9var link = require('@spark-web/link');
10var ts = require('@spark-web/utils/ts');
11var text = require('@spark-web/text');
12var theme = require('@spark-web/theme');
13var internal = require('@spark-web/utils/internal');
14var jsxRuntime = require('react/jsx-runtime');
15var utils = require('@spark-web/utils');
16var react = require('react');
17
18var TEXT_LINK_ERROR_MESSAGE = 'TextLink components must be inside `Text`.';
19function useTextLink(tag) {
20 var textContext = text.useTextContext();
21
22 // Limit API surface area; expect style inheritance
23 if (!textContext) {
24 throw new Error(TEXT_LINK_ERROR_MESSAGE);
25 }
26 var theme$1 = theme.useTheme();
27 var textColor = text.useForegroundTone(textContext.tone);
28 var resetStyles = internal.resetElementStyles(tag);
29 var linkStyles = {
30 color: textColor,
31 cursor: 'pointer',
32 textDecoration: 'underline',
33 fontWeight: theme$1.typography.fontWeight.semibold
34 };
35 var styles = [resetStyles, linkStyles];
36 return styles;
37}
38
39var _excluded$1 = ["as", "data"];
40/**
41 * Text links are used as navigational elements. They may appear on their own,
42 * within a sentence or paragraph, or directly following content.
43 *
44 * @note If you are **only** providing "onClick" use `TextLinkButton` instead.
45 */
46var TextLink = ts.forwardRefWithAs(
47// NOTE: we need `forwardRefWithAs` for TS, but we don't want consumers changing the underlying element
48// eslint-disable-next-line @typescript-eslint/no-unused-vars
49function (_ref, ref) {
50 _ref.as;
51 var data = _ref.data,
52 consumerProps = _objectWithoutProperties(_ref, _excluded$1);
53 var LinkComponent = link.useLinkComponent(ref);
54 var styles = useTextLink('a');
55 return /*#__PURE__*/jsxRuntime.jsx(box.Box, _objectSpread({
56 as: LinkComponent,
57 asElement: "a",
58 ref: ref,
59 className: css.css(styles),
60 data: data
61 }, consumerProps));
62});
63
64var _excluded = ["data"];
65// NOTE: Rather than a native `button` element, we render a `span` with the ARIA
66// role of "button" to avoid issues with text behaviour. Resolves:
67// - alignment
68// - truncating
69// - wrapping
70
71/** The appearance of `TextLink`, with the semantics of a `<button/>`. */
72var TextLinkButton = /*#__PURE__*/react.forwardRef(function (_ref, forwardedRef) {
73 var data = _ref.data,
74 consumerProps = _objectWithoutProperties(_ref, _excluded);
75 var styles = useTextLink('span');
76 var internalRef = react.useRef(null);
77 var ref = utils.useComposedRefs(internalRef, forwardedRef);
78 var handleKeyDown = react.useCallback(function (event) {
79 if (event.key === 'Enter' || event.key === ' ') {
80 var _internalRef$current;
81 event.preventDefault();
82 (_internalRef$current = internalRef.current) === null || _internalRef$current === void 0 ? void 0 : _internalRef$current.click();
83 }
84 }, [internalRef]);
85 return /*#__PURE__*/jsxRuntime.jsx("span", _objectSpread(_objectSpread({
86 role: "button",
87 ref: ref,
88 className: css.css(styles),
89 tabIndex: 0,
90 onKeyDown: handleKeyDown
91 }, data ? internal.buildDataAttributes(data) : undefined), consumerProps));
92});
93TextLinkButton.displayName = 'TextLinkButton';
94
95exports.TextLink = TextLink;
96exports.TextLinkButton = TextLinkButton;