@paul-portfolio/react 0.2.0 → 0.4.0

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/dist/Avatar.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { jsx as _jsx } from "react/jsx-runtime";
2
2
  import { cx } from './cx';
3
- export function Avatar({ src, alt, size, fallback }) {
3
+ export function Avatar({ src, alt, size = 'md', fallback }) {
4
4
  return (_jsx("div", { className: cx('avatar', size && `avatar--${size}`), children: src ? (_jsx("img", { src: src, alt: alt })) : (_jsx("span", { className: "avatar--fallback", children: fallback })) }));
5
5
  }
package/dist/Chip.d.ts CHANGED
@@ -1,10 +1,17 @@
1
- import type { HTMLAttributes } from 'react';
2
- type ChipProps = HTMLAttributes<HTMLSpanElement> & {
1
+ import type { HTMLAttributes, MouseEvent } from 'react';
2
+ type ChipProps = Omit<HTMLAttributes<HTMLSpanElement>, 'onClick'> & {
3
3
  label: string;
4
+ /** Background color (any CSS color). Text flips to white when set. */
5
+ color?: string;
4
6
  size?: 'sm' | 'md';
7
+ /** Stretch to fill the container (e.g. a grid cell). */
8
+ fullWidth?: boolean;
5
9
  clickable?: boolean;
6
10
  removable?: boolean;
11
+ /** When set, the label becomes a real button. */
12
+ onClick?: (e: MouseEvent<HTMLButtonElement>) => void;
7
13
  onRemove?: () => void;
14
+ title?: string;
8
15
  };
9
- export declare function Chip({ label, size, clickable, removable, onClick, onRemove, className, ...props }: ChipProps): import("react").JSX.Element;
16
+ export declare function Chip({ label, color, size, fullWidth, clickable, removable, onClick, onRemove, title, className, ...props }: ChipProps): import("react").JSX.Element;
10
17
  export {};
package/dist/Chip.js CHANGED
@@ -1,8 +1,20 @@
1
1
  import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
2
  import { cx } from './cx';
3
- export function Chip({ label, size, clickable, removable, onClick, onRemove, className, ...props }) {
4
- return (_jsxs("span", { className: cx('chip', size && size !== 'md' && `chip--${size}`, clickable && 'chip--clickable', removable && 'chip--removable', className), onClick: onClick, ...props, children: [label, removable && (_jsx("button", { type: "button", className: "chip__remove", "aria-label": "Remove", onClick: (e) => {
5
- e.stopPropagation();
6
- onRemove?.();
7
- } }))] }));
3
+ export function Chip({ label, color, size, fullWidth, clickable, removable, onClick, onRemove, title, className, ...props }) {
4
+ const showRemove = removable || !!onRemove;
5
+ const interactive = clickable || !!onClick;
6
+ const classes = cx('chip', size && size !== 'md' && `chip--${size}`, interactive && 'chip--clickable', showRemove && 'chip--removable', fullWidth && 'chip--full-width', className);
7
+ const style = color
8
+ ? { backgroundColor: color, color: '#fff' }
9
+ : undefined;
10
+ const remove = showRemove ? (_jsx("button", { type: "button", className: "chip__remove", "aria-label": `Remove ${label}`, onClick: (e) => {
11
+ e.stopPropagation();
12
+ onRemove?.();
13
+ }, children: _jsx("span", { "aria-hidden": "true", children: "\u00D7" }) })) : null;
14
+ // A clickable chip puts the label in its own button so it's keyboard
15
+ // reachable, with the remove button as a sibling (no button-in-button).
16
+ if (onClick) {
17
+ return (_jsxs("span", { className: classes, style: style, title: title, ...props, children: [_jsx("button", { type: "button", className: "chip__label", onClick: onClick, children: label }), remove] }));
18
+ }
19
+ return (_jsxs("span", { className: classes, style: style, title: title, ...props, children: [label, remove] }));
8
20
  }
package/dist/InfoTip.d.ts CHANGED
@@ -1,13 +1,19 @@
1
+ import type { ReactNode } from 'react';
1
2
  type InfoTipProps = {
2
- /** The explanatory text shown in the tooltip. */
3
- content: string;
3
+ /** The explanation shown in the popover. Text or rich nodes. */
4
+ content: ReactNode;
4
5
  side?: 'top' | 'bottom' | 'left' | 'right';
5
6
  /** Accessible name for the trigger. Defaults to "More information". */
6
7
  label?: string;
8
+ /** Max width of the popover in px. */
9
+ maxWidth?: number;
10
+ /** Delay before showing, in ms. */
11
+ delay?: number;
7
12
  };
8
13
  /**
9
- * A small "i" glyph that reveals a tooltip on hover or focus. A convenience
10
- * wrapper over Tooltip for the very common "explain this label" case.
14
+ * A small "i" glyph that reveals a popover on hover or focus. Built on Tooltip,
15
+ * so it renders at a fixed position (never clipped) and accepts rich content —
16
+ * the common "explain this label" case, with room for a few lines of detail.
11
17
  */
12
- export declare function InfoTip({ content, side, label }: InfoTipProps): import("react").JSX.Element;
18
+ export declare function InfoTip({ content, side, label, maxWidth, delay, }: InfoTipProps): import("react").JSX.Element;
13
19
  export {};
package/dist/InfoTip.js CHANGED
@@ -1,9 +1,10 @@
1
1
  import { jsx as _jsx } from "react/jsx-runtime";
2
2
  import { Tooltip } from './Tooltip';
3
3
  /**
4
- * A small "i" glyph that reveals a tooltip on hover or focus. A convenience
5
- * wrapper over Tooltip for the very common "explain this label" case.
4
+ * A small "i" glyph that reveals a popover on hover or focus. Built on Tooltip,
5
+ * so it renders at a fixed position (never clipped) and accepts rich content —
6
+ * the common "explain this label" case, with room for a few lines of detail.
6
7
  */
7
- export function InfoTip({ content, side = 'top', label = 'More information' }) {
8
- return (_jsx(Tooltip, { content: content, side: side, children: _jsx("span", { className: "info-tip", role: "img", "aria-label": label, tabIndex: 0, children: "i" }) }));
8
+ export function InfoTip({ content, side = 'top', label = 'More information', maxWidth, delay, }) {
9
+ return (_jsx(Tooltip, { content: content, side: side, maxWidth: maxWidth, delay: delay, children: _jsx("span", { className: "info-tip", role: "img", "aria-label": label, tabIndex: 0, children: "i" }) }));
9
10
  }
package/dist/Modal.d.ts CHANGED
@@ -3,6 +3,13 @@ type ModalProps = {
3
3
  open: boolean;
4
4
  onClose: () => void;
5
5
  title?: string;
6
+ /** Accessible name when there's no title to point at. */
7
+ 'aria-label'?: string;
8
+ /** Id of an element that labels the dialog (wins over title). */
9
+ 'aria-labelledby'?: string;
10
+ /** Id of an element that describes the dialog. */
11
+ 'aria-describedby'?: string;
12
+ className?: string;
6
13
  children: ReactNode;
7
14
  };
8
15
  declare function Header({ children }: {
@@ -14,7 +21,7 @@ declare function Body({ children }: {
14
21
  declare function Footer({ children }: {
15
22
  children: ReactNode;
16
23
  }): import("react").JSX.Element;
17
- export declare function Modal({ open, onClose, title, children }: ModalProps): import("react").ReactPortal | null;
24
+ export declare function Modal({ open, onClose, title, className, children, 'aria-label': ariaLabel, 'aria-labelledby': ariaLabelledby, 'aria-describedby': ariaDescribedby, }: ModalProps): import("react").ReactPortal | null;
18
25
  export declare namespace Modal {
19
26
  export { Header };
20
27
  export { Body };
package/dist/Modal.js CHANGED
@@ -1,6 +1,7 @@
1
1
  import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
- import { useEffect, useId } from 'react';
2
+ import { useEffect, useId, useRef } from 'react';
3
3
  import { createPortal } from 'react-dom';
4
+ import { cx } from './cx';
4
5
  function Header({ children }) {
5
6
  return _jsx("div", { className: "modal__header", children: children });
6
7
  }
@@ -10,21 +11,51 @@ function Body({ children }) {
10
11
  function Footer({ children }) {
11
12
  return _jsx("div", { className: "modal__footer", children: children });
12
13
  }
13
- export function Modal({ open, onClose, title, children }) {
14
+ const FOCUSABLE = 'a[href], button:not([disabled]), input:not([disabled]), select:not([disabled]), textarea:not([disabled]), [tabindex]:not([tabindex="-1"])';
15
+ export function Modal({ open, onClose, title, className, children, 'aria-label': ariaLabel, 'aria-labelledby': ariaLabelledby, 'aria-describedby': ariaDescribedby, }) {
14
16
  const titleId = useId();
17
+ const dialogRef = useRef(null);
18
+ // Move focus into the dialog on open and restore it on close, and trap Tab
19
+ // so keyboard focus can't wander behind the modal.
15
20
  useEffect(() => {
16
21
  if (!open)
17
22
  return;
23
+ const previouslyFocused = document.activeElement;
24
+ const dialog = dialogRef.current;
25
+ dialog?.focus();
18
26
  function handleKey(e) {
19
- if (e.key === 'Escape')
27
+ if (e.key === 'Escape') {
20
28
  onClose();
29
+ return;
30
+ }
31
+ if (e.key !== 'Tab' || !dialog)
32
+ return;
33
+ const focusables = Array.from(dialog.querySelectorAll(FOCUSABLE));
34
+ if (focusables.length === 0) {
35
+ e.preventDefault();
36
+ return;
37
+ }
38
+ const first = focusables[0];
39
+ const last = focusables[focusables.length - 1];
40
+ if (e.shiftKey && document.activeElement === first) {
41
+ e.preventDefault();
42
+ last.focus();
43
+ }
44
+ else if (!e.shiftKey && document.activeElement === last) {
45
+ e.preventDefault();
46
+ first.focus();
47
+ }
21
48
  }
22
49
  document.addEventListener('keydown', handleKey);
23
- return () => document.removeEventListener('keydown', handleKey);
50
+ return () => {
51
+ document.removeEventListener('keydown', handleKey);
52
+ previouslyFocused?.focus?.();
53
+ };
24
54
  }, [open, onClose]);
25
55
  if (!open)
26
56
  return null;
27
- return createPortal(_jsx("div", { className: "modal__backdrop", onClick: onClose, children: _jsxs("div", { role: "dialog", "aria-modal": "true", "aria-labelledby": title ? titleId : undefined, className: "modal", onClick: (e) => e.stopPropagation(), children: [title && (_jsx("div", { id: titleId, className: "modal__title", children: title })), children] }) }), document.body);
57
+ const labelledby = ariaLabelledby ?? (title ? titleId : undefined);
58
+ return createPortal(_jsx("div", { className: "modal__backdrop", onClick: onClose, children: _jsxs("div", { ref: dialogRef, role: "dialog", "aria-modal": "true", "aria-label": !labelledby ? ariaLabel : undefined, "aria-labelledby": labelledby, "aria-describedby": ariaDescribedby, tabIndex: -1, className: cx('modal__content', className), onClick: (e) => e.stopPropagation(), children: [title && (_jsx("div", { id: titleId, className: "modal__header", children: title })), children] }) }), document.body);
28
59
  }
29
60
  Modal.Header = Header;
30
61
  Modal.Body = Body;
package/dist/Skeleton.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import { jsx as _jsx } from "react/jsx-runtime";
2
2
  import { cx } from './cx';
3
3
  export function Skeleton({ variant, width, height }) {
4
- const style = variant === 'rect'
4
+ const style = width || height
5
5
  ? {
6
6
  '--skeleton-w': width,
7
7
  '--skeleton-h': height,
@@ -1,11 +1,16 @@
1
1
  import { type TextareaHTMLAttributes } from 'react';
2
2
  /**
3
- * Multi-line text field. Mirrors Input's label/error/helper API so the two
4
- * feel the same in a form.
3
+ * Multi-line text field. Mirrors Input's label/error/helper API, and adds an
4
+ * optional hidden label, a required marker, and a live character counter when
5
+ * paired with maxLength.
5
6
  */
6
7
  export declare const Textarea: import("react").ForwardRefExoticComponent<TextareaHTMLAttributes<HTMLTextAreaElement> & {
7
8
  label?: string;
8
9
  error?: string;
9
10
  helper?: string;
11
+ /** Visually hide the label while keeping it available to screen readers. */
12
+ hideLabel?: boolean;
13
+ /** Show a live "used / max" character count. Needs maxLength to be set. */
14
+ showCount?: boolean;
10
15
  disabled?: boolean;
11
16
  } & import("react").RefAttributes<HTMLTextAreaElement>>;
package/dist/Textarea.js CHANGED
@@ -1,13 +1,27 @@
1
1
  import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
- import { forwardRef, useId } from 'react';
2
+ import { forwardRef, useId, useState, } from 'react';
3
3
  import { cx } from './cx';
4
4
  /**
5
- * Multi-line text field. Mirrors Input's label/error/helper API so the two
6
- * feel the same in a form.
5
+ * Multi-line text field. Mirrors Input's label/error/helper API, and adds an
6
+ * optional hidden label, a required marker, and a live character counter when
7
+ * paired with maxLength.
7
8
  */
8
- export const Textarea = forwardRef(function Textarea({ label, error, helper, disabled, className, ...props }, ref) {
9
+ export const Textarea = forwardRef(function Textarea({ label, error, helper, hideLabel = false, showCount = false, required, disabled, maxLength, value, defaultValue, onChange, className, ...props }, ref) {
9
10
  const id = useId();
10
11
  const helperId = `${id}-helper`;
12
+ const countId = `${id}-count`;
11
13
  const helperText = error || helper;
12
- return (_jsxs("div", { className: "input__wrapper", children: [label && (_jsx("label", { className: "input__label", htmlFor: id, children: label })), _jsx("textarea", { ref: ref, id: id, className: cx('textarea', error && 'textarea--error', className), disabled: disabled, "aria-invalid": error ? true : undefined, "aria-describedby": helperText ? helperId : undefined, ...props }), helperText && (_jsx("span", { id: helperId, className: cx('input__helper', error && 'input__helper--error'), children: helperText }))] }));
14
+ // Count is derived from a controlled value, or tracked locally otherwise.
15
+ const controlled = value !== undefined;
16
+ const [localCount, setLocalCount] = useState(() => String(defaultValue ?? '').length);
17
+ const count = controlled ? String(value ?? '').length : localCount;
18
+ const withCount = showCount && maxLength != null;
19
+ const describedBy = [helperText ? helperId : null, withCount ? countId : null]
20
+ .filter(Boolean)
21
+ .join(' ') || undefined;
22
+ return (_jsxs("div", { className: "input__wrapper", children: [label && (_jsxs("label", { className: hideLabel ? 'sr-only' : 'input__label', htmlFor: id, children: [label, required && (_jsx("span", { "aria-hidden": "true", children: " *" }))] })), _jsx("textarea", { ref: ref, id: id, className: cx('textarea', error && 'textarea--error', className), disabled: disabled, required: required, maxLength: maxLength, value: value, defaultValue: defaultValue, "aria-invalid": error ? true : undefined, "aria-describedby": describedBy, onChange: (e) => {
23
+ if (!controlled)
24
+ setLocalCount(e.target.value.length);
25
+ onChange?.(e);
26
+ }, ...props }), withCount && (_jsxs("span", { id: countId, className: "textarea__count", "aria-live": "polite", children: [count, " / ", maxLength] })), helperText && (_jsx("span", { id: helperId, className: cx('input__helper', error && 'input__helper--error'), children: helperText }))] }));
13
27
  });
package/dist/Tooltip.d.ts CHANGED
@@ -1,8 +1,19 @@
1
1
  import { type ReactNode } from 'react';
2
+ type TooltipSide = 'top' | 'bottom' | 'left' | 'right';
2
3
  type TooltipProps = {
3
- content: string;
4
- side?: 'top' | 'bottom' | 'left' | 'right';
4
+ /** Content shown in the floating label. Text or rich nodes. */
5
+ content: ReactNode;
6
+ side?: TooltipSide;
7
+ /** Delay before showing, in ms. Avoids flashing on a quick mouse pass. */
8
+ delay?: number;
9
+ /** Max width of the bubble in px. */
10
+ maxWidth?: number;
5
11
  children: ReactNode;
6
12
  };
7
- export declare function Tooltip({ content, side, children }: TooltipProps): import("react").JSX.Element;
13
+ /**
14
+ * A tooltip that renders at a fixed screen position, so it's never clipped by
15
+ * an overflow:hidden ancestor (grids, cards, chips) and needs no portal. Shows
16
+ * on hover and focus after `delay` ms; Escape dismisses it.
17
+ */
18
+ export declare function Tooltip({ content, side, delay, maxWidth, children }: TooltipProps): import("react").JSX.Element;
8
19
  export {};
package/dist/Tooltip.js CHANGED
@@ -1,8 +1,63 @@
1
1
  import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
- import { useState, useId } from 'react';
2
+ import { useState, useRef, useCallback, useId, } from 'react';
3
3
  import { cx } from './cx';
4
- export function Tooltip({ content, side = 'top', children }) {
5
- const [visible, setVisible] = useState(false);
4
+ const GAP = 8;
5
+ /**
6
+ * A tooltip that renders at a fixed screen position, so it's never clipped by
7
+ * an overflow:hidden ancestor (grids, cards, chips) and needs no portal. Shows
8
+ * on hover and focus after `delay` ms; Escape dismisses it.
9
+ */
10
+ export function Tooltip({ content, side = 'top', delay = 500, maxWidth, children }) {
6
11
  const id = useId();
7
- return (_jsxs("span", { onMouseEnter: () => setVisible(true), onMouseLeave: () => setVisible(false), "aria-describedby": id, style: { position: 'relative', display: 'inline-block' }, children: [children, _jsx("span", { id: id, role: "tooltip", className: cx('tooltip', `tooltip--${side}`, visible && 'tooltip--visible'), children: content })] }));
12
+ const [visible, setVisible] = useState(false);
13
+ const [rect, setRect] = useState(null);
14
+ const timer = useRef(null);
15
+ const show = useCallback((el) => {
16
+ setRect(el.getBoundingClientRect());
17
+ if (timer.current)
18
+ clearTimeout(timer.current);
19
+ timer.current = setTimeout(() => setVisible(true), delay);
20
+ }, [delay]);
21
+ const hide = useCallback(() => {
22
+ if (timer.current)
23
+ clearTimeout(timer.current);
24
+ setVisible(false);
25
+ }, []);
26
+ const style = rect
27
+ ? { position: 'fixed', ...place(rect, side), maxWidth }
28
+ : undefined;
29
+ return (_jsxs("span", { className: "tooltip__anchor", style: { display: 'inline-flex' }, onMouseEnter: (e) => show(e.currentTarget), onMouseLeave: hide, onFocus: (e) => show(e.currentTarget), onBlur: hide, onKeyDown: (e) => {
30
+ if (e.key === 'Escape' && visible)
31
+ hide();
32
+ }, "aria-describedby": visible ? id : undefined, children: [children, visible && rect && (_jsx("span", { id: id, role: "tooltip", className: cx('tooltip', `tooltip--${side}`, 'tooltip--visible'), style: style, children: content }))] }));
33
+ }
34
+ /** Screen coordinates + transform to anchor the bubble on a side of the rect. */
35
+ function place(rect, side) {
36
+ switch (side) {
37
+ case 'bottom':
38
+ return {
39
+ left: rect.left + rect.width / 2,
40
+ top: rect.bottom + GAP,
41
+ transform: 'translateX(-50%)',
42
+ };
43
+ case 'left':
44
+ return {
45
+ left: rect.left - GAP,
46
+ top: rect.top + rect.height / 2,
47
+ transform: 'translate(-100%, -50%)',
48
+ };
49
+ case 'right':
50
+ return {
51
+ left: rect.right + GAP,
52
+ top: rect.top + rect.height / 2,
53
+ transform: 'translateY(-50%)',
54
+ };
55
+ case 'top':
56
+ default:
57
+ return {
58
+ left: rect.left + rect.width / 2,
59
+ top: rect.top - GAP,
60
+ transform: 'translate(-50%, -100%)',
61
+ };
62
+ }
8
63
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@paul-portfolio/react",
3
- "version": "0.2.0",
3
+ "version": "0.4.0",
4
4
  "description": "React components for the Paul Design System",
5
5
  "license": "MIT",
6
6
  "repository": {