UNPKG

3.35 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, '__esModule', { value: true });
4
5var css = require('@emotion/css');
6var a11y = require('@spark-web/a11y');
7var box = require('@spark-web/box');
8var link = require('@spark-web/link');
9var text = require('@spark-web/text');
10var theme = require('@spark-web/theme');
11var react = require('react');
12var jsxRuntime = require('react/jsx-runtime');
13
14var NavLink = /*#__PURE__*/react.forwardRef(function (_ref, forwardedRef) {
15 var _ref$borderRadius = _ref.borderRadius,
16 borderRadius = _ref$borderRadius === void 0 ? 'small' : _ref$borderRadius,
17 children = _ref.children,
18 data = _ref.data,
19 href = _ref.href,
20 _ref$inline = _ref.inline,
21 inline = _ref$inline === void 0 ? false : _ref$inline,
22 _ref$isSelected = _ref.isSelected,
23 isSelected = _ref$isSelected === void 0 ? false : _ref$isSelected,
24 _ref$size = _ref.size,
25 size = _ref$size === void 0 ? 'medium' : _ref$size;
26 var linkComponent = link.useLinkComponent(forwardedRef);
27 var styles = useNavLinkStyles(isSelected);
28 return /*#__PURE__*/jsxRuntime.jsx(box.Box, {
29 as: linkComponent,
30 asElement: "a",
31 href: href,
32 "aria-current": isSelected ? 'page' : undefined,
33 data: data
34 // styles
35 ,
36 background: isSelected ? 'primaryMuted' : undefined,
37 display: inline ? 'inline-flex' : 'flex',
38 alignItems: "center",
39 gap: "small",
40 paddingY: "small",
41 paddingX: "medium",
42 borderRadius: {
43 tablet: borderRadius
44 },
45 className: css.css(styles),
46 children: resolveNavLinkChildren({
47 children: children,
48 isSelected: isSelected,
49 size: size
50 })
51 });
52});
53NavLink.displayName = 'NavLink';
54function useNavLinkStyles(isSelected) {
55 var theme$1 = theme.useTheme();
56 var focusRingStyles = a11y.useFocusRing();
57 return {
58 ':focus': focusRingStyles,
59 ':hover': {
60 backgroundColor: isSelected ? theme$1.backgroundInteractions.primaryLowHover : theme$1.color.background.surfaceMuted,
61 '> *': {
62 color: isSelected ? theme$1.color.foreground.primaryHover : undefined,
63 stroke: isSelected ? theme$1.color.foreground.primaryHover : undefined
64 }
65 },
66 ':active': {
67 backgroundColor: isSelected ? theme$1.backgroundInteractions.positiveLowActive : theme$1.color.background.surfacePressed,
68 '> *': {
69 color: isSelected ? theme$1.color.foreground.primaryActive : undefined,
70 stroke: isSelected ? theme$1.color.foreground.primaryActive : undefined
71 }
72 }
73 };
74}
75function resolveNavLinkChildren(_ref2) {
76 var children = _ref2.children,
77 isSelected = _ref2.isSelected,
78 size = _ref2.size;
79 return react.Children.map(children, function (child) {
80 if (typeof child === 'string') {
81 return /*#__PURE__*/jsxRuntime.jsx(text.Text, {
82 as: "span",
83 baseline: false,
84 overflowStrategy: "nowrap",
85 weight: "semibold",
86 size: mapTextSize[size],
87 tone: isSelected ? 'primaryActive' : 'muted',
88 children: child
89 });
90 }
91 if ( /*#__PURE__*/react.isValidElement(child)) {
92 return /*#__PURE__*/react.cloneElement(child, {
93 size: 'xxsmall',
94 tone: isSelected ? 'primaryActive' : 'muted'
95 });
96 }
97 return null;
98 });
99}
100var mapTextSize = {
101 medium: 'small',
102 large: 'standard'
103};
104
105exports.NavLink = NavLink;