@nicolassancho/storybook-core 4.3.0 → 4.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -36,7 +36,7 @@ documented in Storybook. It is published to npm as `@nicolassancho/storybook-cor
36
36
  | Layer | Components |
37
37
  | --------- | ----------------------------------------------------------------------------------- |
38
38
  | Atoms | Button, ButtonLink, Card, Chip, ChipLink, Icon, Image, Label, RadioButton, SearchInput (deprecated), Spinner, Text |
39
- | Molecules | Alert, Breadcrumbs, CartIcon, Input, ProductCard, ProductDetails, RadioGroup |
39
+ | Molecules | Alert, Breadcrumbs, CartIcon, Input, Popover, ProductCard, ProductDetails, RadioGroup, SidebarNav |
40
40
  | Organisms | Footer, Header, ProductGrid |
41
41
  | Layouts | Container, Grid |
42
42
 
@@ -17,7 +17,7 @@ export declare const buttonStyles: import("tailwind-variants").TVReturnType<{
17
17
  true: "no-underline";
18
18
  false: "";
19
19
  };
20
- }, undefined, "font-bold inline-flex items-center focus:outline-hidden focus-visible:ring-2 focus-visible:ring-primary focus-visible:ring-offset-2 focus-visible:ring-offset-surface", {
20
+ }, undefined, "font-bold inline-flex items-center enabled:cursor-pointer focus:outline-hidden focus-visible:ring-2 focus-visible:ring-primary focus-visible:ring-offset-2 focus-visible:ring-offset-surface", {
21
21
  variant: {
22
22
  primary: "rounded-sm bg-primary text-on-primary";
23
23
  secondary: "rounded-sm bg-secondary text-on-secondary focus-visible:ring-secondary";
@@ -2,7 +2,7 @@ import { tv } from "tailwind-variants";
2
2
  // Shared by Button and ButtonLink, not exported from `src/index.js`. The hover classes are in
3
3
  // `compoundVariants` because `enabled:` never matches an `<a>`.
4
4
  export const buttonStyles = tv({
5
- base: "font-bold inline-flex items-center focus:outline-hidden focus-visible:ring-2 focus-visible:ring-primary focus-visible:ring-offset-2 focus-visible:ring-offset-surface",
5
+ base: "font-bold inline-flex items-center enabled:cursor-pointer focus:outline-hidden focus-visible:ring-2 focus-visible:ring-primary focus-visible:ring-offset-2 focus-visible:ring-offset-surface",
6
6
  variants: {
7
7
  variant: {
8
8
  primary: "rounded-sm bg-primary text-on-primary",
@@ -6,7 +6,7 @@ export declare const chipStyles: import("tailwind-variants").TVReturnType<{
6
6
  disabled: {
7
7
  true: "pointer-events-none cursor-not-allowed opacity-50";
8
8
  };
9
- }, undefined, "inline-flex items-center rounded-full border px-4 py-2 text-sm font-medium no-underline transition-colors focus:outline-hidden focus-visible:ring-2 focus-visible:ring-primary focus-visible:ring-offset-2 focus-visible:ring-offset-surface", {
9
+ }, undefined, "inline-flex items-center enabled:cursor-pointer rounded-full border px-4 py-2 text-sm font-medium no-underline transition-colors focus:outline-hidden focus-visible:ring-2 focus-visible:ring-primary focus-visible:ring-offset-2 focus-visible:ring-offset-surface", {
10
10
  selected: {
11
11
  true: "border-primary bg-primary text-on-primary hover:bg-primary-darker";
12
12
  false: "border-neutral-400 bg-surface text-neutral-900 hover:border-neutral-900";
@@ -2,7 +2,7 @@ import { tv } from "tailwind-variants";
2
2
  // Shared by Chip (a button) and ChipLink (a link) so the two look the same. It is not exported
3
3
  // from `src/index.js`: it is an implementation detail, not public API.
4
4
  export const chipStyles = tv({
5
- base: "inline-flex items-center rounded-full border px-4 py-2 text-sm font-medium no-underline transition-colors focus:outline-hidden focus-visible:ring-2 focus-visible:ring-primary focus-visible:ring-offset-2 focus-visible:ring-offset-surface",
5
+ base: "inline-flex items-center enabled:cursor-pointer rounded-full border px-4 py-2 text-sm font-medium no-underline transition-colors focus:outline-hidden focus-visible:ring-2 focus-visible:ring-primary focus-visible:ring-offset-2 focus-visible:ring-offset-surface",
6
6
  variants: {
7
7
  selected: {
8
8
  true: "border-primary bg-primary text-on-primary hover:bg-primary-darker",
@@ -8,6 +8,9 @@ export declare const IconsMap: {
8
8
  AlertCircle: SvgIcon;
9
9
  AlertOctagon: SvgIcon;
10
10
  AlertTriangle: SvgIcon;
11
+ Menu: SvgIcon;
12
+ Package: SvgIcon;
13
+ X: SvgIcon;
11
14
  Sun: SvgIcon;
12
15
  Moon: SvgIcon;
13
16
  };
@@ -9,6 +9,9 @@ const ShoppingCart = createIcon(_jsxs(_Fragment, { children: [_jsx("circle", { c
9
9
  const ArrowDown = createIcon(_jsxs(_Fragment, { children: [_jsx("line", { x1: "12", y1: "5", x2: "12", y2: "19" }), _jsx("polyline", { points: "19 12 12 19 5 12" })] }));
10
10
  const ArrowLeft = createIcon(_jsxs(_Fragment, { children: [_jsx("line", { x1: "19", y1: "12", x2: "5", y2: "12" }), _jsx("polyline", { points: "12 19 5 12 12 5" })] }));
11
11
  const AlertOctagon = createIcon(_jsxs(_Fragment, { children: [_jsx("polygon", { points: "7.86 2 16.14 2 22 7.86 22 16.14 16.14 22 7.86 22 2 16.14 2 7.86 7.86 2" }), _jsx("line", { x1: "12", y1: "8", x2: "12", y2: "12" }), _jsx("line", { x1: "12", y1: "16", x2: "12.01", y2: "16" })] }));
12
+ const Package = createIcon(_jsxs(_Fragment, { children: [_jsx("line", { x1: "16.5", y1: "9.4", x2: "7.5", y2: "4.21" }), _jsx("path", { d: "M21 16V8a2 2 0 0 0-1-1.73l-7-4a2 2 0 0 0-2 0l-7 4A2 2 0 0 0 3 8v8a2 2 0 0 0 1 1.73l7 4a2 2 0 0 0 2 0l7-4A2 2 0 0 0 21 16z" }), _jsx("polyline", { points: "3.27 6.96 12 12.01 20.73 6.96" }), _jsx("line", { x1: "12", y1: "22.08", x2: "12", y2: "12" })] }));
13
+ const Menu = createIcon(_jsxs(_Fragment, { children: [_jsx("line", { x1: "3", y1: "12", x2: "21", y2: "12" }), _jsx("line", { x1: "3", y1: "6", x2: "21", y2: "6" }), _jsx("line", { x1: "3", y1: "18", x2: "21", y2: "18" })] }));
14
+ const X = createIcon(_jsxs(_Fragment, { children: [_jsx("line", { x1: "18", y1: "6", x2: "6", y2: "18" }), _jsx("line", { x1: "6", y1: "6", x2: "18", y2: "18" })] }));
12
15
  const AlertCircle = createIcon(_jsxs(_Fragment, { children: [_jsx("circle", { cx: "12", cy: "12", r: "10" }), _jsx("line", { x1: "12", y1: "8", x2: "12", y2: "12" }), _jsx("line", { x1: "12", y1: "16", x2: "12.01", y2: "16" })] }));
13
16
  const AlertTriangle = createIcon(_jsxs(_Fragment, { children: [_jsx("path", { d: "M10.29 3.86L1.82 18a2 2 0 0 0 1.71 3h16.94a2 2 0 0 0 1.71-3L13.71 3.86a2 2 0 0 0-3.42 0z" }), _jsx("line", { x1: "12", y1: "9", x2: "12", y2: "13" }), _jsx("line", { x1: "12", y1: "17", x2: "12.01", y2: "17" })] }));
14
17
  const Sun = createIcon(_jsxs(_Fragment, { children: [_jsx("circle", { cx: "12", cy: "12", r: "5" }), _jsx("line", { x1: "12", y1: "1", x2: "12", y2: "3" }), _jsx("line", { x1: "12", y1: "21", x2: "12", y2: "23" }), _jsx("line", { x1: "4.22", y1: "4.22", x2: "5.64", y2: "5.64" }), _jsx("line", { x1: "18.36", y1: "18.36", x2: "19.78", y2: "19.78" }), _jsx("line", { x1: "1", y1: "12", x2: "3", y2: "12" }), _jsx("line", { x1: "21", y1: "12", x2: "23", y2: "12" }), _jsx("line", { x1: "4.22", y1: "19.78", x2: "5.64", y2: "18.36" }), _jsx("line", { x1: "18.36", y1: "5.64", x2: "19.78", y2: "4.22" })] }));
@@ -21,6 +24,9 @@ export const IconsMap = {
21
24
  AlertCircle,
22
25
  AlertOctagon,
23
26
  AlertTriangle,
27
+ Menu,
28
+ Package,
29
+ X,
24
30
  Sun,
25
31
  Moon,
26
32
  };
@@ -0,0 +1,42 @@
1
+ import React from "react";
2
+ export interface PopoverProps {
3
+ /** Content of the button that opens the panel: text, an icon, or both */
4
+ trigger: React.ReactNode;
5
+ /**
6
+ * Accessible name for the trigger. Needed only when `trigger` has no text, for example an
7
+ * icon-only button. It replaces the visible text for screen readers, so leave it out otherwise.
8
+ */
9
+ triggerLabel?: string;
10
+ /** Style of the trigger button, one of the `Button` variants */
11
+ variant?: "primary" | "secondary" | "text" | "outline";
12
+ /** Size of the trigger button */
13
+ size?: "small" | "medium" | "large";
14
+ /**
15
+ * Where the panel appears. `bottom-start` and `bottom-end` open a dropdown under the trigger,
16
+ * aligned to its left or right edge. `drawer` slides a full-height panel in from the left edge of
17
+ * the screen, with a dimmed backdrop. The drawer is not modal: keyboard focus can still leave it.
18
+ */
19
+ placement?: "bottom-start" | "bottom-end" | "drawer";
20
+ /**
21
+ * Accessible name of the close button that the `drawer` placement shows at the top of the panel,
22
+ * for people who cannot press Esc or tap the dimmed area. Translate it for other languages. A
23
+ * dropdown has no close button.
24
+ */
25
+ closeLabel?: string;
26
+ /**
27
+ * Panel content. It can be Server Components, so a navigation list or a form with a Server
28
+ * Action stays on the server. Only the trigger button and the panel shell are client code.
29
+ */
30
+ children: React.ReactNode;
31
+ /** Additional classes for the panel */
32
+ className?: string;
33
+ }
34
+ /**
35
+ * A button that opens a panel using the browser's native `popover`. The browser handles closing on
36
+ * an outside click and on Esc, returning focus to the button, and drawing the panel above the page.
37
+ *
38
+ * This is a client component. It adds what the native popover lacks: `aria-expanded` on the button,
39
+ * closing when a link inside the panel is clicked or a form inside it is submitted, and placing a
40
+ * dropdown under its button. Render it from a Server Component; every prop is serializable.
41
+ */
42
+ export declare const Popover: React.FC<PopoverProps>;
@@ -0,0 +1,70 @@
1
+ "use client";
2
+ import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
3
+ import { useCallback, useEffect, useId, useRef, useState } from "react";
4
+ import { tv } from "tailwind-variants";
5
+ import { buttonStyles } from "../../Atoms/Button/buttonStyles.js";
6
+ import { Icon } from "../../Atoms/Icon/Icon.js";
7
+ const popoverStyles = tv({
8
+ base: "m-0 border-neutral-200 bg-surface text-neutral-900",
9
+ variants: {
10
+ placement: {
11
+ "bottom-start": "inset-auto w-max min-w-48 max-w-[calc(100vw-2rem)] rounded-lg border p-2 shadow-lg backdrop:bg-transparent",
12
+ "bottom-end": "inset-auto w-max min-w-48 max-w-[calc(100vw-2rem)] rounded-lg border p-2 shadow-lg backdrop:bg-transparent",
13
+ drawer: "inset-y-0 left-0 h-dvh w-72 max-w-[85vw] overflow-y-auto border-r p-0 shadow-lg backdrop:bg-black/40",
14
+ },
15
+ },
16
+ defaultVariants: { placement: "bottom-start" },
17
+ });
18
+ const GAP = 4;
19
+ /**
20
+ * A button that opens a panel using the browser's native `popover`. The browser handles closing on
21
+ * an outside click and on Esc, returning focus to the button, and drawing the panel above the page.
22
+ *
23
+ * This is a client component. It adds what the native popover lacks: `aria-expanded` on the button,
24
+ * closing when a link inside the panel is clicked or a form inside it is submitted, and placing a
25
+ * dropdown under its button. Render it from a Server Component; every prop is serializable.
26
+ */
27
+ export const Popover = ({ trigger, triggerLabel, variant = "outline", size = "medium", placement = "bottom-start", closeLabel = "Close menu", children, className, }) => {
28
+ const panelId = useId();
29
+ const triggerRef = useRef(null);
30
+ const panelRef = useRef(null);
31
+ const [open, setOpen] = useState(false);
32
+ // A popover is drawn in the top layer, so it ignores its parent's position: place it from the
33
+ // trigger's rectangle in the viewport.
34
+ const place = useCallback(() => {
35
+ const panel = panelRef.current;
36
+ const button = triggerRef.current;
37
+ if (!panel || !button || placement === "drawer")
38
+ return;
39
+ const rect = button.getBoundingClientRect();
40
+ panel.style.top = `${rect.bottom + GAP}px`;
41
+ if (placement === "bottom-end") {
42
+ panel.style.left = "auto";
43
+ panel.style.right = `${document.documentElement.clientWidth - rect.right}px`;
44
+ }
45
+ else {
46
+ panel.style.right = "auto";
47
+ panel.style.left = `${rect.left}px`;
48
+ }
49
+ }, [placement]);
50
+ useEffect(() => {
51
+ if (!open)
52
+ return;
53
+ window.addEventListener("resize", place);
54
+ window.addEventListener("scroll", place, true);
55
+ return () => {
56
+ window.removeEventListener("resize", place);
57
+ window.removeEventListener("scroll", place, true);
58
+ };
59
+ }, [open, place]);
60
+ const closeOnNavigation = (event) => {
61
+ var _a;
62
+ if (event.target instanceof Element && event.target.closest("a[href]")) {
63
+ (_a = panelRef.current) === null || _a === void 0 ? void 0 : _a.hidePopover();
64
+ }
65
+ };
66
+ return (_jsxs(_Fragment, { children: [_jsx("button", { ref: triggerRef, type: "button", popoverTarget: panelId, "aria-expanded": open, "aria-controls": panelId, "aria-label": triggerLabel, className: buttonStyles({ variant, size }), children: trigger }), _jsxs("div", { ref: panelRef, id: panelId, popover: "auto", onBeforeToggle: (event) => {
67
+ if (event.newState === "open")
68
+ place();
69
+ }, onToggle: (event) => setOpen(event.newState === "open"), onClick: closeOnNavigation, onSubmit: () => { var _a; return (_a = panelRef.current) === null || _a === void 0 ? void 0 : _a.hidePopover(); }, className: popoverStyles({ placement, className }), children: [placement === "drawer" && (_jsx("div", { className: "flex justify-end p-2", children: _jsx("button", { type: "button", popoverTarget: panelId, popoverTargetAction: "hide", "aria-label": closeLabel, className: buttonStyles({ variant: "outline", size: "small" }), children: _jsx(Icon, { name: "X", size: "small" }) }) })), children] })] }));
70
+ };
@@ -0,0 +1,42 @@
1
+ import React from "react";
2
+ import { IconProps } from "../../Atoms/Icon/Icon.js";
3
+ export interface SidebarNavItem {
4
+ /** Link text */
5
+ label: string;
6
+ /** Where the link goes. It needs no JavaScript. */
7
+ href: string;
8
+ /** Name of an icon shown before the label. It is decorative: the label names the link. */
9
+ icon?: IconProps["name"];
10
+ /**
11
+ * Whether this is the page the user is on. It sets `aria-current="page"` and highlights the item.
12
+ * The component cannot know the URL, so the app decides, usually with `usePathname`.
13
+ */
14
+ current?: boolean;
15
+ }
16
+ export interface SidebarNavProps {
17
+ /** The links, in order */
18
+ items: SidebarNavItem[];
19
+ /** Accessible name of the navigation landmark. Name it when the page has more than one `nav`. */
20
+ label?: string;
21
+ /** Content above the links, for example the tenant's logo and name */
22
+ header?: React.ReactNode;
23
+ /**
24
+ * Component used to render each link. Receives `href`, `className`, `children` and
25
+ * `aria-current`, so a framework link (e.g. `next/link`) can be passed directly. Defaults to a
26
+ * plain `<a>`. Define it outside the render function: an inline definition remounts the links.
27
+ */
28
+ LinkComponent?: React.ElementType<{
29
+ href: string;
30
+ children: React.ReactNode;
31
+ className?: string;
32
+ "aria-current"?: "page";
33
+ }>;
34
+ /** Additional classes for the wrapper */
35
+ className?: string;
36
+ }
37
+ /**
38
+ * The navigation list for an app's side panel: labelled links with an optional icon, and the current
39
+ * page highlighted with the brand color. It has no width, position or collapse behavior of its own:
40
+ * the app's layout decides those. Put it inside a `Popover` with `placement="drawer"` on narrow screens.
41
+ */
42
+ export declare const SidebarNav: React.FC<SidebarNavProps>;
@@ -0,0 +1,28 @@
1
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ import { tv } from "tailwind-variants";
3
+ import { Icon } from "../../Atoms/Icon/Icon.js";
4
+ const sidebarNavStyles = tv({
5
+ slots: {
6
+ base: "flex flex-col gap-4",
7
+ list: "flex flex-col gap-1",
8
+ link: "flex items-center gap-2 rounded-sm border-l-2 px-3 py-2 text-sm font-medium text-neutral-900 focus:outline-hidden focus-visible:ring-2 focus-visible:ring-primary focus-visible:ring-offset-2 focus-visible:ring-offset-surface",
9
+ },
10
+ variants: {
11
+ current: {
12
+ true: { link: "border-primary bg-primary/10" },
13
+ false: { link: "border-transparent hover:bg-neutral-100" },
14
+ },
15
+ },
16
+ defaultVariants: { current: false },
17
+ });
18
+ /**
19
+ * The navigation list for an app's side panel: labelled links with an optional icon, and the current
20
+ * page highlighted with the brand color. It has no width, position or collapse behavior of its own:
21
+ * the app's layout decides those. Put it inside a `Popover` with `placement="drawer"` on narrow screens.
22
+ */
23
+ export const SidebarNav = ({ items, label = "Main", header, LinkComponent = "a", className, }) => {
24
+ const styles = sidebarNavStyles();
25
+ // When some items have an icon, the others reserve its space so every label lines up.
26
+ const hasIcons = items.some(({ icon }) => icon);
27
+ return (_jsxs("div", { className: styles.base({ className }), children: [header, _jsx("nav", { "aria-label": label, children: _jsx("ul", { className: styles.list(), children: items.map(({ label: text, href, icon, current }) => (_jsx("li", { children: _jsxs(LinkComponent, { href: href, className: sidebarNavStyles({ current: Boolean(current) }).link(), "aria-current": current ? "page" : undefined, children: [icon ? (_jsx(Icon, { name: icon, size: "small" })) : (hasIcons && _jsx("span", { "aria-hidden": "true", className: "size-4 shrink-0" })), _jsx("span", { children: text })] }) }, href))) }) })] }));
28
+ };
package/dist/index.d.ts CHANGED
@@ -16,9 +16,11 @@ export * from "./components/Molecules/Alert/Alert.js";
16
16
  export * from "./components/Molecules/Breadcrumbs/Breadcrumbs.js";
17
17
  export * from "./components/Molecules/CartIcon/CartIcon.js";
18
18
  export * from "./components/Molecules/Input/Input.js";
19
+ export * from "./components/Molecules/Popover/Popover.js";
19
20
  export * from "./components/Molecules/ProductCard/ProductCard.js";
20
21
  export * from "./components/Molecules/ProductDetails/ProductDetails.js";
21
22
  export * from "./components/Molecules/RadioGroup/RadioGroup.js";
23
+ export * from "./components/Molecules/SidebarNav/SidebarNav.js";
22
24
  export * from "./components/Organisms/Footer/Footer.js";
23
25
  export * from "./components/Organisms/Header/Header.js";
24
26
  export * from "./components/Organisms/ProductGrid/ProductGrid.js";
package/dist/index.js CHANGED
@@ -19,9 +19,11 @@ export * from "./components/Molecules/Alert/Alert.js";
19
19
  export * from "./components/Molecules/Breadcrumbs/Breadcrumbs.js";
20
20
  export * from "./components/Molecules/CartIcon/CartIcon.js";
21
21
  export * from "./components/Molecules/Input/Input.js";
22
+ export * from "./components/Molecules/Popover/Popover.js";
22
23
  export * from "./components/Molecules/ProductCard/ProductCard.js";
23
24
  export * from "./components/Molecules/ProductDetails/ProductDetails.js";
24
25
  export * from "./components/Molecules/RadioGroup/RadioGroup.js";
26
+ export * from "./components/Molecules/SidebarNav/SidebarNav.js";
25
27
  // Organisms
26
28
  export * from "./components/Organisms/Footer/Footer.js";
27
29
  export * from "./components/Organisms/Header/Header.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nicolassancho/storybook-core",
3
- "version": "4.3.0",
3
+ "version": "4.4.1",
4
4
  "description": "Accessible React + Tailwind component library built with tailwind-variants and documented in Storybook.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",