feat(island): sync components with the Lumina design system
New components: - Lightbox, NetworkStatus (+ useOnline), TierOption, SkeletonPreset Rebuilt to the design system: - PredictionCard: range bar, open KPIs, closed hero, price ladder, horizon progress, seal, locked veil - CreatorStats: form curve, outcome split, KPI tiles, insights and streak; null renders nothing - PostCard, LockedTeaser, MediaCarousel: owner action rules, expanded variant, viewer CTA, locked order - SubscriptionCard: active/suspended/expired/refunded, compact density, amount and support rows - Countdown, ResendCode, OtpInput: absolute end times, DS wording and thresholds, WebOTP Fixes: - Dialog: waits only for finite exit animations, keeps non-dismissible halts open, focuses the panel, closes on scrim presses only, mounts the panel only while open - Toaster: moves its region into the open dialog so toasts show and stay tappable over sheets - Button: blocked submit, frozen cursor, loading links; TextInput focus border; Card, Banner, ChoiceCard, StepProgress, ErrorState, Amount and Avatar details - Tokens: muted status tracks, inverse controls, scrim chip, inverse and lightbox focus rings; the spinner keeps turning under reduced motion Adds sonner and svgo; island gets the icons target and the hooks subpath. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
|
@ -28,6 +28,7 @@
|
||||||
"oxlint": "^1.75.0",
|
"oxlint": "^1.75.0",
|
||||||
"postcss": "^8.5.28",
|
"postcss": "^8.5.28",
|
||||||
"prettier": "~3.6.2",
|
"prettier": "~3.6.2",
|
||||||
|
"svgo": "^4.1.0",
|
||||||
"tailwindcss": "^4.3.3",
|
"tailwindcss": "^4.3.3",
|
||||||
"tslib": "^2.8.1",
|
"tslib": "^2.8.1",
|
||||||
"typescript": "^6.0.3",
|
"typescript": "^6.0.3",
|
||||||
|
|
@ -41,6 +42,7 @@
|
||||||
"react": "^19.0.0",
|
"react": "^19.0.0",
|
||||||
"react-dom": "^19.0.0",
|
"react-dom": "^19.0.0",
|
||||||
"server-only": "^0.0.1",
|
"server-only": "^0.0.1",
|
||||||
|
"sonner": "^2.0.8",
|
||||||
"tailwind-merge": "^3.7.0",
|
"tailwind-merge": "^3.7.0",
|
||||||
"valibot": "^1.5.0"
|
"valibot": "^1.5.0"
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -14,6 +14,7 @@
|
||||||
"./organisms/*": "./src/organisms/*/index.ts",
|
"./organisms/*": "./src/organisms/*/index.ts",
|
||||||
"./templates/*": "./src/templates/*/index.ts",
|
"./templates/*": "./src/templates/*/index.ts",
|
||||||
"./primitives/*": "./src/primitives/*/index.ts",
|
"./primitives/*": "./src/primitives/*/index.ts",
|
||||||
|
"./hooks/*": "./src/hooks/*/index.ts",
|
||||||
"./tools/*": "./src/tools/*/index.ts",
|
"./tools/*": "./src/tools/*/index.ts",
|
||||||
"./env": "./src/env/index.ts",
|
"./env": "./src/env/index.ts",
|
||||||
"./styles/*": "./src/styles/*",
|
"./styles/*": "./src/styles/*",
|
||||||
|
|
@ -26,7 +27,24 @@
|
||||||
"tags": [
|
"tags": [
|
||||||
"type:ui",
|
"type:ui",
|
||||||
"scope:shared"
|
"scope:shared"
|
||||||
]
|
],
|
||||||
|
"targets": {
|
||||||
|
"icons": {
|
||||||
|
"command": "jiti src/tools/icon-sprite-plugin/run.ts",
|
||||||
|
"options": {
|
||||||
|
"cwd": "{projectRoot}"
|
||||||
|
},
|
||||||
|
"inputs": [
|
||||||
|
"{projectRoot}/src/icons/*.svg",
|
||||||
|
"{projectRoot}/src/tools/icon-sprite-plugin/**/*"
|
||||||
|
],
|
||||||
|
"outputs": [
|
||||||
|
"{projectRoot}/src/atoms/icon/sprite.svg",
|
||||||
|
"{projectRoot}/src/atoms/icon/icon-names.ts"
|
||||||
|
],
|
||||||
|
"cache": true
|
||||||
|
}
|
||||||
|
}
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@lumina/libs": "workspace:*",
|
"@lumina/libs": "workspace:*",
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,71 @@
|
||||||
|
import { number, signed as signedNumber } from '@lumina/tools/format';
|
||||||
|
|
||||||
|
import { cn } from '../../tools/cn';
|
||||||
|
import { FlexBox } from '../flex-box';
|
||||||
|
import { Typography } from '../typography';
|
||||||
|
import { amountWeight } from './constants';
|
||||||
|
import type { AmountProps } from './types';
|
||||||
|
import {
|
||||||
|
amountCurrencyVariants,
|
||||||
|
amountNumberVariants,
|
||||||
|
amountVariants,
|
||||||
|
} from './variants';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Toman amount. The number sits in an LTR isolate, so a sign stays beside the digits in RTL
|
||||||
|
* text and the unit stays outside it. The font draws the digits in Persian. The root sets the
|
||||||
|
* size, so the unit can be 0.8em of it.
|
||||||
|
*/
|
||||||
|
export function Amount({
|
||||||
|
value,
|
||||||
|
size,
|
||||||
|
tone = size === 'figure' ? 'figure' : 'ink',
|
||||||
|
struck = false,
|
||||||
|
signed = false,
|
||||||
|
decimals = 0,
|
||||||
|
currency,
|
||||||
|
srLabel,
|
||||||
|
className,
|
||||||
|
...props
|
||||||
|
}: AmountProps) {
|
||||||
|
const weight = amountWeight({ size, tone, struck });
|
||||||
|
|
||||||
|
return (
|
||||||
|
<FlexBox
|
||||||
|
tag="span"
|
||||||
|
display="inline-flex"
|
||||||
|
alignItems="items-baseline"
|
||||||
|
gap="gap-1"
|
||||||
|
className={cn(amountVariants({ size, tone, struck }), className)}
|
||||||
|
{...props}
|
||||||
|
>
|
||||||
|
<Typography
|
||||||
|
tag="bdi"
|
||||||
|
dir="ltr"
|
||||||
|
variant="inherit"
|
||||||
|
color="inherit"
|
||||||
|
className={amountNumberVariants({ weight, struck })}
|
||||||
|
>
|
||||||
|
{signed ? signedNumber(value, decimals) : number(value, decimals)}
|
||||||
|
</Typography>
|
||||||
|
{currency != null && (
|
||||||
|
<Typography
|
||||||
|
tag="span"
|
||||||
|
variant="inherit"
|
||||||
|
color="inherit"
|
||||||
|
className={amountCurrencyVariants({
|
||||||
|
muted: (tone === 'ink' || tone === 'figure') && !struck,
|
||||||
|
})}
|
||||||
|
>
|
||||||
|
{currency}
|
||||||
|
</Typography>
|
||||||
|
)}
|
||||||
|
{srLabel && (
|
||||||
|
// A leading space and brackets, so it reads as an aside after the amount.
|
||||||
|
<Typography tag="span" className="sr-only">
|
||||||
|
{` (${srLabel})`}
|
||||||
|
</Typography>
|
||||||
|
)}
|
||||||
|
</FlexBox>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,21 @@
|
||||||
|
import type { AmountProps } from './types';
|
||||||
|
|
||||||
|
type AmountWeight = 'regular' | 'semibold' | 'bold';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The number's weight, in the design system's cascade order (the last matching rule wins):
|
||||||
|
* figure 700, then title 600, mute 400, positive 600, struck 400. So `mute` and `struck` stay
|
||||||
|
* regular even at figure size.
|
||||||
|
*/
|
||||||
|
export function amountWeight({
|
||||||
|
size,
|
||||||
|
tone,
|
||||||
|
struck,
|
||||||
|
}: Pick<AmountProps, 'size' | 'tone' | 'struck'>): AmountWeight {
|
||||||
|
if (struck) return 'regular';
|
||||||
|
if (tone === 'positive') return 'semibold';
|
||||||
|
if (tone === 'mute') return 'regular';
|
||||||
|
if (size === 'title') return 'semibold';
|
||||||
|
if (size === 'figure' || tone === 'figure') return 'bold';
|
||||||
|
return 'semibold';
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,3 @@
|
||||||
|
export { Amount } from './Amount';
|
||||||
|
export type { AmountProps } from './types';
|
||||||
|
export { amountVariants } from './variants';
|
||||||
|
|
@ -0,0 +1,17 @@
|
||||||
|
import type { VariantProps } from 'class-variance-authority';
|
||||||
|
import type { ComponentPropsWithRef, ReactNode } from 'react';
|
||||||
|
|
||||||
|
import type { amountVariants } from './variants';
|
||||||
|
|
||||||
|
export type AmountProps = Omit<ComponentPropsWithRef<'span'>, 'children'> &
|
||||||
|
VariantProps<typeof amountVariants> & {
|
||||||
|
/** Whole Toman. Never Rial, never rounded or abbreviated. */
|
||||||
|
value: number;
|
||||||
|
/** Always shows `+` or `−` (ledger lines). The colour stays; the sign carries the meaning. */
|
||||||
|
signed?: boolean;
|
||||||
|
decimals?: number;
|
||||||
|
/** The unit after the number, such as «تومان». Omit it where a column header names the unit. */
|
||||||
|
currency?: ReactNode;
|
||||||
|
/** Hidden text that says why, such as «لغو شده» on a struck amount. */
|
||||||
|
srLabel?: string;
|
||||||
|
};
|
||||||
|
|
@ -0,0 +1,52 @@
|
||||||
|
import { cva } from 'class-variance-authority';
|
||||||
|
|
||||||
|
export const amountVariants = cva('whitespace-nowrap', {
|
||||||
|
variants: {
|
||||||
|
size: {
|
||||||
|
/** The one hero figure on a screen: قابل واریز, the checkout total. */
|
||||||
|
figure: 'text-display',
|
||||||
|
/** Tier totals. */
|
||||||
|
title: 'text-heading-sm',
|
||||||
|
body: 'text-body-md',
|
||||||
|
/** Dense rows. */
|
||||||
|
caption: 'text-caption',
|
||||||
|
},
|
||||||
|
tone: {
|
||||||
|
ink: 'text-text-primary',
|
||||||
|
/** The hero tone: ink with a bold number (600 at title size, as in the DS). Default at figure size. */
|
||||||
|
figure: 'text-text-primary',
|
||||||
|
/** Per-month equivalents. */
|
||||||
|
mute: 'text-text-secondary',
|
||||||
|
/** A negative balance, with its explanation underneath. */
|
||||||
|
negative: 'text-danger-text',
|
||||||
|
/** Money returned to the investor (بازگشت وجه), with `signed`. */
|
||||||
|
positive: 'text-success-text',
|
||||||
|
},
|
||||||
|
/** A cancelled payout, or a standing price beside a promotion. */
|
||||||
|
struck: { true: 'text-text-tertiary', false: '' },
|
||||||
|
},
|
||||||
|
defaultVariants: { size: 'body', tone: 'ink', struck: false },
|
||||||
|
});
|
||||||
|
|
||||||
|
export const amountNumberVariants = cva('', {
|
||||||
|
variants: {
|
||||||
|
weight: {
|
||||||
|
regular: 'font-normal',
|
||||||
|
semibold: 'font-semibold',
|
||||||
|
bold: 'font-bold',
|
||||||
|
},
|
||||||
|
struck: {
|
||||||
|
true: 'line-through decoration-[1.5px]',
|
||||||
|
false: '',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
defaultVariants: { weight: 'semibold', struck: false },
|
||||||
|
});
|
||||||
|
|
||||||
|
export const amountCurrencyVariants = cva('text-[0.8em] font-normal', {
|
||||||
|
variants: {
|
||||||
|
/** «تومان» is secondary next to an ink figure, and follows any other tone. */
|
||||||
|
muted: { true: 'text-text-secondary', false: '' },
|
||||||
|
},
|
||||||
|
defaultVariants: { muted: true },
|
||||||
|
});
|
||||||
|
|
@ -0,0 +1,78 @@
|
||||||
|
import { cn } from '../../tools/cn';
|
||||||
|
import { Element } from '../../primitives/element';
|
||||||
|
import { GridBox } from '../grid-box';
|
||||||
|
import { Icon } from '../icon';
|
||||||
|
import { Typography } from '../typography';
|
||||||
|
import { AVATAR_INITIAL_VARIANT, AVATAR_MARK_PX, AVATAR_PX } from './constants';
|
||||||
|
import type { AvatarProps } from './types';
|
||||||
|
import { avatarVariants } from './variants';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A circular creator or user picture. The placeholder sits under the image, so it shows while
|
||||||
|
* the image loads and when it fails, without client code.
|
||||||
|
*/
|
||||||
|
export function Avatar({
|
||||||
|
name,
|
||||||
|
src,
|
||||||
|
size = 'md',
|
||||||
|
verified = false,
|
||||||
|
verifiedLabel,
|
||||||
|
label,
|
||||||
|
className,
|
||||||
|
...props
|
||||||
|
}: AvatarProps) {
|
||||||
|
const px = AVATAR_PX[size];
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Element
|
||||||
|
tag="span"
|
||||||
|
className={cn('relative inline-block flex-none', className)}
|
||||||
|
{...props}
|
||||||
|
>
|
||||||
|
<GridBox
|
||||||
|
tag="span"
|
||||||
|
role={label ? 'img' : undefined}
|
||||||
|
aria-label={label}
|
||||||
|
aria-hidden={label ? undefined : true}
|
||||||
|
justifyItems="justify-items-center"
|
||||||
|
alignItems="items-center"
|
||||||
|
className={avatarVariants({ size })}
|
||||||
|
>
|
||||||
|
<Typography
|
||||||
|
tag="span"
|
||||||
|
variant={AVATAR_INITIAL_VARIANT[size]}
|
||||||
|
color="inherit"
|
||||||
|
className="font-bold uppercase"
|
||||||
|
>
|
||||||
|
{Array.from(name)[0] ?? '?'}
|
||||||
|
</Typography>
|
||||||
|
{src && (
|
||||||
|
// A broken image with an empty alt draws nothing, so the letter shows through.
|
||||||
|
<Element
|
||||||
|
tag="img"
|
||||||
|
src={src}
|
||||||
|
alt=""
|
||||||
|
width={px}
|
||||||
|
height={px}
|
||||||
|
loading="lazy"
|
||||||
|
decoding="async"
|
||||||
|
className="absolute inset-0 size-full object-cover"
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
</GridBox>
|
||||||
|
{verified && (
|
||||||
|
<GridBox
|
||||||
|
tag="span"
|
||||||
|
justifyItems="justify-items-center"
|
||||||
|
alignItems="items-center"
|
||||||
|
role={verifiedLabel ? 'img' : undefined}
|
||||||
|
aria-label={verifiedLabel}
|
||||||
|
aria-hidden={verifiedLabel ? undefined : true}
|
||||||
|
className="absolute -end-0.5 -bottom-0.5 rounded-full bg-action-primary p-0.5 text-text-on-action ring-2 ring-surface"
|
||||||
|
>
|
||||||
|
<Icon name="check" size={AVATAR_MARK_PX[size]} strokeWidth={3} />
|
||||||
|
</GridBox>
|
||||||
|
)}
|
||||||
|
</Element>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,24 @@
|
||||||
|
import type { TypographyVariant } from '../typography';
|
||||||
|
import type { AvatarSize } from './types';
|
||||||
|
|
||||||
|
/** px, so the image reserves its box before it loads. */
|
||||||
|
export const AVATAR_PX: Record<AvatarSize, number> = {
|
||||||
|
sm: 32,
|
||||||
|
md: 40,
|
||||||
|
lg: 56,
|
||||||
|
xl: 80,
|
||||||
|
};
|
||||||
|
|
||||||
|
export const AVATAR_INITIAL_VARIANT: Record<AvatarSize, TypographyVariant> = {
|
||||||
|
sm: 'label',
|
||||||
|
md: 'body-md',
|
||||||
|
lg: 'heading-sm',
|
||||||
|
xl: 'heading-lg',
|
||||||
|
};
|
||||||
|
|
||||||
|
export const AVATAR_MARK_PX: Record<AvatarSize, number> = {
|
||||||
|
sm: 12,
|
||||||
|
md: 14,
|
||||||
|
lg: 18,
|
||||||
|
xl: 22,
|
||||||
|
};
|
||||||
|
|
@ -0,0 +1,3 @@
|
||||||
|
export { Avatar } from './Avatar';
|
||||||
|
export type { AvatarProps, AvatarSize } from './types';
|
||||||
|
export { avatarVariants } from './variants';
|
||||||
|
|
@ -0,0 +1,25 @@
|
||||||
|
import type { VariantProps } from 'class-variance-authority';
|
||||||
|
import type { ComponentPropsWithRef } from 'react';
|
||||||
|
|
||||||
|
import type { avatarVariants } from './variants';
|
||||||
|
|
||||||
|
export type AvatarSize = NonNullable<
|
||||||
|
VariantProps<typeof avatarVariants>['size']
|
||||||
|
>;
|
||||||
|
|
||||||
|
export type AvatarProps = Omit<ComponentPropsWithRef<'span'>, 'children'> & {
|
||||||
|
/** The username: its first letter is the placeholder, or `?` while it is empty. */
|
||||||
|
name: string;
|
||||||
|
/** The uploaded image; omit it to show the placeholder. */
|
||||||
|
src?: string | null;
|
||||||
|
size?: AvatarSize;
|
||||||
|
/** The creator's verification mark at the bottom inline end. */
|
||||||
|
verified?: boolean;
|
||||||
|
/** The mark's accessible name, such as «تأییدشده». */
|
||||||
|
verifiedLabel?: string;
|
||||||
|
/**
|
||||||
|
* The avatar's accessible name. Omit it when the username is next to it, as on a post header,
|
||||||
|
* so screen readers don't read the name twice.
|
||||||
|
*/
|
||||||
|
label?: string;
|
||||||
|
};
|
||||||
|
|
@ -0,0 +1,21 @@
|
||||||
|
import { cva } from 'class-variance-authority';
|
||||||
|
|
||||||
|
/** The placeholder is the username's first letter in white on `action-primary`. */
|
||||||
|
export const avatarVariants = cva(
|
||||||
|
'relative flex-none overflow-hidden rounded-full bg-action-primary text-text-on-action',
|
||||||
|
{
|
||||||
|
variants: {
|
||||||
|
size: {
|
||||||
|
/** Compact rows and the top bar. */
|
||||||
|
sm: 'size-8',
|
||||||
|
/** Post headers, subscription cards, list rows. */
|
||||||
|
md: 'size-10',
|
||||||
|
/** Sheet and checkout headers. */
|
||||||
|
lg: 'size-14',
|
||||||
|
/** The channel profile header. */
|
||||||
|
xl: 'size-20',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
defaultVariants: { size: 'md' },
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
@ -0,0 +1,109 @@
|
||||||
|
import NextLink from 'next/link';
|
||||||
|
import type { MouseEvent } from 'react';
|
||||||
|
|
||||||
|
import { cn } from '../../tools/cn';
|
||||||
|
import { FlexBox } from '../flex-box';
|
||||||
|
import { ButtonContent } from './components/ButtonContent';
|
||||||
|
import { FrozenButton } from './components/FrozenButton';
|
||||||
|
import { buttonLayout, withoutLinkProps } from './constants';
|
||||||
|
import type { ButtonLinkElementProps, ButtonProps } from './types';
|
||||||
|
import { buttonVariants } from './variants';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Every tappable action: a full pill (an icon-only button is a circle). No hooks, so a server
|
||||||
|
* component can render it with no JavaScript; only a frozen button with a reason is a client one.
|
||||||
|
*/
|
||||||
|
export function Button({
|
||||||
|
variant,
|
||||||
|
size,
|
||||||
|
block,
|
||||||
|
icon,
|
||||||
|
iconEnd,
|
||||||
|
label,
|
||||||
|
frozen = false,
|
||||||
|
frozenReason,
|
||||||
|
isLoading = false,
|
||||||
|
className,
|
||||||
|
children,
|
||||||
|
...props
|
||||||
|
}: ButtonProps) {
|
||||||
|
const iconOnly = children == null && icon !== undefined;
|
||||||
|
const classes = cn(
|
||||||
|
buttonVariants({ variant, size, block, iconOnly, frozen }),
|
||||||
|
className,
|
||||||
|
);
|
||||||
|
const content = (
|
||||||
|
<ButtonContent icon={icon} iconEnd={iconEnd} isLoading={isLoading}>
|
||||||
|
{children}
|
||||||
|
</ButtonContent>
|
||||||
|
);
|
||||||
|
const ariaLabel = iconOnly ? label : props['aria-label'];
|
||||||
|
const title = iconOnly && !frozen ? label : undefined;
|
||||||
|
const isBlocked = frozen || isLoading;
|
||||||
|
|
||||||
|
// A frozen or loading link renders a `<button type="button">` below. An `<a>` would need a click
|
||||||
|
// handler to stop it navigating, which a server render can't attach; dropping its href would
|
||||||
|
// also drop it from the tab order and lose focus. A button stays focused and announces busy.
|
||||||
|
if (props.href !== undefined && !isBlocked) {
|
||||||
|
const linkProps = props as ButtonLinkElementProps;
|
||||||
|
return (
|
||||||
|
<FlexBox
|
||||||
|
tag={NextLink}
|
||||||
|
{...buttonLayout(block)}
|
||||||
|
{...linkProps}
|
||||||
|
className={classes}
|
||||||
|
aria-label={ariaLabel}
|
||||||
|
title={title}
|
||||||
|
>
|
||||||
|
{content}
|
||||||
|
</FlexBox>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const { type = 'button', onClick, ...buttonProps } = withoutLinkProps(props);
|
||||||
|
|
||||||
|
if (frozen && frozenReason != null) {
|
||||||
|
return (
|
||||||
|
<FrozenButton
|
||||||
|
{...buttonProps}
|
||||||
|
reason={frozenReason}
|
||||||
|
block={block}
|
||||||
|
className={classes}
|
||||||
|
aria-label={ariaLabel}
|
||||||
|
>
|
||||||
|
{content}
|
||||||
|
</FrozenButton>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// With onClick (only a client component can pass one) the handler is always attached and, while
|
||||||
|
// blocked, swallows the click: preventDefault also stops a submit button's form, including Enter
|
||||||
|
// in a field. A server render can't carry a handler, so a blocked button without onClick renders
|
||||||
|
// `type="button"` instead, which can't submit its form at all.
|
||||||
|
const handleClick =
|
||||||
|
onClick &&
|
||||||
|
((event: MouseEvent<HTMLButtonElement>) => {
|
||||||
|
if (isBlocked) {
|
||||||
|
event.preventDefault();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
onClick(event);
|
||||||
|
});
|
||||||
|
|
||||||
|
return (
|
||||||
|
<FlexBox
|
||||||
|
tag="button"
|
||||||
|
{...buttonLayout(block)}
|
||||||
|
{...buttonProps}
|
||||||
|
type={isBlocked && !onClick ? 'button' : type}
|
||||||
|
className={classes}
|
||||||
|
onClick={handleClick}
|
||||||
|
aria-label={ariaLabel}
|
||||||
|
aria-disabled={isBlocked || undefined}
|
||||||
|
aria-busy={isLoading || undefined}
|
||||||
|
title={title}
|
||||||
|
>
|
||||||
|
{content}
|
||||||
|
</FlexBox>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,23 @@
|
||||||
|
import { Icon } from '../../icon';
|
||||||
|
import { Spinner } from '../../spinner';
|
||||||
|
import { Typography } from '../../typography';
|
||||||
|
import type { ButtonContentProps } from '../types';
|
||||||
|
|
||||||
|
export function ButtonContent({
|
||||||
|
icon,
|
||||||
|
iconEnd,
|
||||||
|
isLoading,
|
||||||
|
children,
|
||||||
|
}: ButtonContentProps) {
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
{isLoading ? <Spinner /> : icon && <Icon name={icon} />}
|
||||||
|
{children != null && (
|
||||||
|
<Typography tag="span" variant="inherit" color="inherit">
|
||||||
|
{children}
|
||||||
|
</Typography>
|
||||||
|
)}
|
||||||
|
{iconEnd && <Icon name={iconEnd} />}
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,54 @@
|
||||||
|
'use client';
|
||||||
|
|
||||||
|
import { useId, useState } from 'react';
|
||||||
|
|
||||||
|
import { FlexBox } from '../../flex-box';
|
||||||
|
import { Typography } from '../../typography';
|
||||||
|
import { buttonLayout } from '../constants';
|
||||||
|
import type { FrozenButtonProps } from '../types';
|
||||||
|
import { frozenTipVariants } from '../variants';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A frozen button with its reason. Hover and keyboard focus show the reason through CSS; a tap
|
||||||
|
* toggles it, because touch has no hover. Only frozen buttons with a reason load this code.
|
||||||
|
*/
|
||||||
|
export function FrozenButton({
|
||||||
|
reason,
|
||||||
|
block,
|
||||||
|
children,
|
||||||
|
...props
|
||||||
|
}: FrozenButtonProps) {
|
||||||
|
const [isOpen, setIsOpen] = useState(false);
|
||||||
|
const tipId = useId();
|
||||||
|
|
||||||
|
return (
|
||||||
|
<FlexBox
|
||||||
|
tag="span"
|
||||||
|
display={block ? 'flex' : 'inline-flex'}
|
||||||
|
className="group relative"
|
||||||
|
onMouseLeave={() => setIsOpen(false)}
|
||||||
|
>
|
||||||
|
<FlexBox
|
||||||
|
tag="button"
|
||||||
|
{...buttonLayout(block)}
|
||||||
|
type="button"
|
||||||
|
aria-disabled
|
||||||
|
aria-describedby={tipId}
|
||||||
|
onClick={() => setIsOpen((open) => !open)}
|
||||||
|
{...props}
|
||||||
|
>
|
||||||
|
{children}
|
||||||
|
</FlexBox>
|
||||||
|
<Typography
|
||||||
|
tag="span"
|
||||||
|
id={tipId}
|
||||||
|
role="tooltip"
|
||||||
|
variant="caption"
|
||||||
|
color="inverse"
|
||||||
|
className={frozenTipVariants({ open: isOpen })}
|
||||||
|
>
|
||||||
|
{reason}
|
||||||
|
</Typography>
|
||||||
|
</FlexBox>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,34 @@
|
||||||
|
import type { FlexBoxOwnProps } from '../flex-box';
|
||||||
|
import type { ButtonElementProps } from './types';
|
||||||
|
|
||||||
|
/** Icon, label and end icon in one centred row; `block` stretches it to the full width. */
|
||||||
|
export function buttonLayout(block: boolean | null | undefined) {
|
||||||
|
return {
|
||||||
|
display: block ? 'flex' : 'inline-flex',
|
||||||
|
alignItems: 'items-center',
|
||||||
|
justifyContent: 'justify-center',
|
||||||
|
gap: 'gap-2',
|
||||||
|
} as const satisfies FlexBoxOwnProps;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Props only NextLink understands. */
|
||||||
|
const LINK_ONLY_PROPS = new Set([
|
||||||
|
'href',
|
||||||
|
'as',
|
||||||
|
'replace',
|
||||||
|
'scroll',
|
||||||
|
'shallow',
|
||||||
|
'passHref',
|
||||||
|
'prefetch',
|
||||||
|
'locale',
|
||||||
|
'legacyBehavior',
|
||||||
|
'onNavigate',
|
||||||
|
'unstable_dynamicOnHover',
|
||||||
|
]);
|
||||||
|
|
||||||
|
/** A frozen or loading link renders a `<button>`, which must not receive NextLink's props. */
|
||||||
|
export function withoutLinkProps(props: object): ButtonElementProps {
|
||||||
|
return Object.fromEntries(
|
||||||
|
Object.entries(props).filter(([key]) => !LINK_ONLY_PROPS.has(key)),
|
||||||
|
) as ButtonElementProps;
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,7 @@
|
||||||
|
export { Button } from './Button';
|
||||||
|
export type {
|
||||||
|
ButtonAsButtonProps,
|
||||||
|
ButtonAsLinkProps,
|
||||||
|
ButtonProps,
|
||||||
|
} from './types';
|
||||||
|
export { buttonVariants } from './variants';
|
||||||
|
|
@ -0,0 +1,61 @@
|
||||||
|
import type { VariantProps } from 'class-variance-authority';
|
||||||
|
import type NextLink from 'next/link';
|
||||||
|
import type { ComponentPropsWithRef, ReactNode } from 'react';
|
||||||
|
|
||||||
|
import type { IconName } from '../icon';
|
||||||
|
import type { buttonVariants } from './variants';
|
||||||
|
|
||||||
|
type ButtonOwnProps = Pick<
|
||||||
|
VariantProps<typeof buttonVariants>,
|
||||||
|
'variant' | 'size' | 'block'
|
||||||
|
> & {
|
||||||
|
/** Leading icon; alone (no children) it makes an icon-only circle that needs `label`. */
|
||||||
|
icon?: IconName;
|
||||||
|
iconEnd?: IconName;
|
||||||
|
/** Accessible name of an icon-only button, also shown as its tooltip. */
|
||||||
|
label?: string;
|
||||||
|
/** The action exists but can't be used now: it stays visible at 40% and explains itself. */
|
||||||
|
frozen?: boolean;
|
||||||
|
/** Why the button is frozen, shown on tap, hover and focus. */
|
||||||
|
frozenReason?: ReactNode;
|
||||||
|
/** An action is in flight: keeps the label, shows a spinner, and blocks repeat taps. */
|
||||||
|
isLoading?: boolean;
|
||||||
|
children?: ReactNode;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type ButtonAsButtonProps = ButtonOwnProps &
|
||||||
|
Omit<ComponentPropsWithRef<'button'>, keyof ButtonOwnProps> & {
|
||||||
|
href?: undefined;
|
||||||
|
};
|
||||||
|
|
||||||
|
/** A link that looks like a button. A frozen or loading one renders a button, so it can't navigate. */
|
||||||
|
export type ButtonAsLinkProps = ButtonOwnProps &
|
||||||
|
Omit<ComponentPropsWithRef<typeof NextLink>, keyof ButtonOwnProps> & {
|
||||||
|
href: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type ButtonProps = ButtonAsButtonProps | ButtonAsLinkProps;
|
||||||
|
|
||||||
|
/** What Button passes on to its `<button>` or link after taking its own props. */
|
||||||
|
export type ButtonElementProps = Omit<
|
||||||
|
ButtonAsButtonProps,
|
||||||
|
keyof ButtonOwnProps
|
||||||
|
>;
|
||||||
|
export type ButtonLinkElementProps = Omit<
|
||||||
|
ButtonAsLinkProps,
|
||||||
|
keyof ButtonOwnProps
|
||||||
|
>;
|
||||||
|
|
||||||
|
export type ButtonContentProps = Pick<
|
||||||
|
ButtonOwnProps,
|
||||||
|
'icon' | 'iconEnd' | 'isLoading' | 'children'
|
||||||
|
>;
|
||||||
|
|
||||||
|
export type FrozenButtonProps = Omit<
|
||||||
|
ComponentPropsWithRef<'button'>,
|
||||||
|
'children'
|
||||||
|
> & {
|
||||||
|
reason: ReactNode;
|
||||||
|
block?: boolean | null;
|
||||||
|
children: ReactNode;
|
||||||
|
};
|
||||||
|
|
@ -0,0 +1,61 @@
|
||||||
|
import { cva } from 'class-variance-authority';
|
||||||
|
|
||||||
|
export const buttonVariants = cva(
|
||||||
|
'min-h-control-md min-w-touch-target rounded-full border border-transparent px-5 text-body-md leading-6 font-semibold whitespace-nowrap no-underline transition-colors duration-120 ease-standard focus-visible:focus-ring actionable:cursor-pointer disabled:frozen aria-busy:cursor-progress',
|
||||||
|
{
|
||||||
|
variants: {
|
||||||
|
variant: {
|
||||||
|
primary:
|
||||||
|
'bg-action-primary text-text-on-action actionable:hover:bg-action-primary-hover actionable:active:bg-action-primary-pressed',
|
||||||
|
secondary:
|
||||||
|
'border-border-default bg-surface text-text-primary actionable:hover:border-text-secondary actionable:hover:bg-surface-hover',
|
||||||
|
ghost:
|
||||||
|
'bg-transparent px-3 text-text-secondary actionable:hover:bg-surface-hover actionable:hover:text-text-primary',
|
||||||
|
danger:
|
||||||
|
'border-danger bg-surface text-danger-text actionable:hover:bg-danger-subtle',
|
||||||
|
'danger-solid':
|
||||||
|
'bg-danger-solid text-text-on-action actionable:hover:bg-danger-solid-hover actionable:active:bg-danger-solid-pressed',
|
||||||
|
},
|
||||||
|
size: {
|
||||||
|
sm: 'min-h-control-sm min-w-control-sm px-3 text-label leading-5',
|
||||||
|
md: '',
|
||||||
|
// 17px on the same 24px line: `leading-6` after the size, or cn drops it for heading-sm's 28px.
|
||||||
|
lg: 'min-h-control-lg px-6 text-heading-sm leading-6',
|
||||||
|
},
|
||||||
|
block: {
|
||||||
|
true: 'w-full',
|
||||||
|
false: '',
|
||||||
|
},
|
||||||
|
iconOnly: {
|
||||||
|
true: 'w-touch-target px-0',
|
||||||
|
false: '',
|
||||||
|
},
|
||||||
|
frozen: {
|
||||||
|
true: 'frozen',
|
||||||
|
false: '',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
compoundVariants: [
|
||||||
|
{ size: 'sm', iconOnly: true, className: 'w-control-sm' },
|
||||||
|
],
|
||||||
|
defaultVariants: {
|
||||||
|
variant: 'primary',
|
||||||
|
size: 'md',
|
||||||
|
block: false,
|
||||||
|
iconOnly: false,
|
||||||
|
frozen: false,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
/** The tooltip that explains a frozen button, above it and centred on it. */
|
||||||
|
export const frozenTipVariants = cva(
|
||||||
|
// Physical left: centring is the same in RTL and LTR.
|
||||||
|
'pointer-events-none absolute bottom-[calc(100%+0.5rem)] left-1/2 z-menu hidden w-max max-w-[16.25rem] -translate-x-1/2 rounded-sm bg-surface-inverse px-3 py-1.5 text-center whitespace-normal shadow-2 group-focus-within:block group-hover:block',
|
||||||
|
{
|
||||||
|
variants: {
|
||||||
|
open: { true: 'block', false: '' },
|
||||||
|
},
|
||||||
|
defaultVariants: { open: false },
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
@ -0,0 +1,29 @@
|
||||||
|
import { CountdownView } from './components/CountdownView';
|
||||||
|
import { LiveCountdown } from './components/LiveCountdown';
|
||||||
|
import { DAY_SECONDS } from './constants';
|
||||||
|
import { countdownState } from './label';
|
||||||
|
import type { CountdownProps } from './types';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A time-based label with the brief's escalation rules: the OTP clock, a promotion's end, and a
|
||||||
|
* subscription's time left. No fake urgency: it only turns amber or red when time is short.
|
||||||
|
*
|
||||||
|
* `days` (remaining) or a frozen `now` need no clock, so the label renders here, on the server,
|
||||||
|
* with no JavaScript: a server component may pass its labels. Otherwise it counts live against
|
||||||
|
* the absolute `endsAt` (a remount never restarts it) in a client component, so its labels are
|
||||||
|
* functions only a client parent can pass.
|
||||||
|
*/
|
||||||
|
export function Countdown(props: CountdownProps) {
|
||||||
|
const { kind, endsAt, days, now, labels = {}, prefix, className } = props;
|
||||||
|
const end = endsAt == null ? null : new Date(endsAt).getTime();
|
||||||
|
const usesDays = kind === 'remaining' && days != null;
|
||||||
|
|
||||||
|
if (!usesDays && (now == null || end == null))
|
||||||
|
return <LiveCountdown {...props} />;
|
||||||
|
|
||||||
|
const left = usesDays
|
||||||
|
? days * DAY_SECONDS
|
||||||
|
: Math.max(0, Math.ceil((end! - new Date(now!).getTime()) / 1000));
|
||||||
|
const state = countdownState({ kind, prefix, labels, left, end });
|
||||||
|
return state && <CountdownView {...state} className={className} />;
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,50 @@
|
||||||
|
import { cn } from '../../../tools/cn';
|
||||||
|
import { FlexBox } from '../../flex-box';
|
||||||
|
import { Icon } from '../../icon';
|
||||||
|
import { Typography } from '../../typography';
|
||||||
|
import { COUNTDOWN_ICON } from '../constants';
|
||||||
|
import type { CountdownState } from '../label';
|
||||||
|
import { countdownVariants } from '../variants';
|
||||||
|
|
||||||
|
type CountdownViewProps = CountdownState & {
|
||||||
|
/** A live clock that doesn't know the time yet: an invisible placeholder of the same size. */
|
||||||
|
isPending?: boolean;
|
||||||
|
className?: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
/** The countdown's markup, with no clock: the server render and the live clock both draw it. */
|
||||||
|
export function CountdownView({
|
||||||
|
tone,
|
||||||
|
icon,
|
||||||
|
text,
|
||||||
|
isTimer,
|
||||||
|
isPending = false,
|
||||||
|
className,
|
||||||
|
}: CountdownViewProps) {
|
||||||
|
return (
|
||||||
|
<FlexBox
|
||||||
|
tag="span"
|
||||||
|
display="inline-flex"
|
||||||
|
alignItems="items-center"
|
||||||
|
gap="gap-1"
|
||||||
|
role={isTimer ? 'timer' : undefined}
|
||||||
|
aria-live={isTimer ? 'off' : undefined}
|
||||||
|
aria-hidden={isPending || undefined}
|
||||||
|
className={cn(
|
||||||
|
countdownVariants({ tone }),
|
||||||
|
isPending && 'invisible',
|
||||||
|
className,
|
||||||
|
)}
|
||||||
|
>
|
||||||
|
{icon && <Icon name={icon} {...COUNTDOWN_ICON} />}
|
||||||
|
<Typography
|
||||||
|
tag="span"
|
||||||
|
variant="caption"
|
||||||
|
color="inherit"
|
||||||
|
className="min-h-lh font-semibold"
|
||||||
|
>
|
||||||
|
{text}
|
||||||
|
</Typography>
|
||||||
|
</FlexBox>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,50 @@
|
||||||
|
'use client';
|
||||||
|
|
||||||
|
import { useEffect, useEffectEvent, useRef } from 'react';
|
||||||
|
|
||||||
|
import { useCountdown } from '../../../hooks/use-countdown';
|
||||||
|
import { TICK_MS } from '../constants';
|
||||||
|
import { countdownState } from '../label';
|
||||||
|
import type { CountdownProps } from '../types';
|
||||||
|
import { CountdownView } from './CountdownView';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A countdown that ticks: the OTP clock and a promotion's end. The server and the browser never
|
||||||
|
* agree on the time to the second, so the server renders an invisible placeholder of the same
|
||||||
|
* height and the label fills in once hydrated (client navigations get it on their first render).
|
||||||
|
*/
|
||||||
|
export function LiveCountdown({
|
||||||
|
kind,
|
||||||
|
endsAt,
|
||||||
|
now,
|
||||||
|
labels = {},
|
||||||
|
prefix,
|
||||||
|
onExpire,
|
||||||
|
className,
|
||||||
|
}: CountdownProps) {
|
||||||
|
const left = useCountdown(endsAt, { now, intervalMs: TICK_MS[kind] });
|
||||||
|
const end = endsAt == null ? null : new Date(endsAt).getTime();
|
||||||
|
const isExpired = kind === 'otp' && left === 0;
|
||||||
|
|
||||||
|
// Reads the latest callback without re-firing when the parent passes a new one.
|
||||||
|
const expire = useEffectEvent(() => onExpire?.());
|
||||||
|
// Once per end, even when Strict Mode runs the effect twice.
|
||||||
|
const expiredAt = useRef<number | null>(null);
|
||||||
|
useEffect(() => {
|
||||||
|
if (!isExpired || expiredAt.current === end) return;
|
||||||
|
expiredAt.current = end;
|
||||||
|
expire();
|
||||||
|
}, [isExpired, end]);
|
||||||
|
|
||||||
|
if (end == null) return null;
|
||||||
|
const isPending = left == null;
|
||||||
|
const state = countdownState({ kind, prefix, labels, left, end });
|
||||||
|
// Before the time is known, an empty caption line keeps the row's height.
|
||||||
|
const shown =
|
||||||
|
state ??
|
||||||
|
(isPending ? { tone: 'calm' as const, text: null, isTimer: false } : null);
|
||||||
|
if (!shown) return null;
|
||||||
|
return (
|
||||||
|
<CountdownView {...shown} isPending={isPending} className={className} />
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,24 @@
|
||||||
|
import type { CountdownKind } from './types';
|
||||||
|
|
||||||
|
export const HOUR_SECONDS = 60 * 60;
|
||||||
|
export const DAY_SECONDS = 24 * HOUR_SECONDS;
|
||||||
|
|
||||||
|
/** The OTP clock turns red in its last 30 seconds. */
|
||||||
|
export const OTP_URGENT_SECONDS = 30;
|
||||||
|
/** A promotion counts down in hours from 48 hours; before that it shows a plain date. */
|
||||||
|
export const PROMOTION_URGENT_HOURS = 48;
|
||||||
|
/** A subscription's days left turn amber at 7 and red at 3. */
|
||||||
|
export const REMAINING_WARNING_DAYS = 7;
|
||||||
|
export const REMAINING_DANGER_DAYS = 3;
|
||||||
|
/** 4–7 days never reads lower than 4, so amber always means «more than 3 days». */
|
||||||
|
export const REMAINING_WARNING_MIN_DAYS = REMAINING_DANGER_DAYS + 1;
|
||||||
|
|
||||||
|
/** A clock ticks every second; hours and days only need a minute. */
|
||||||
|
export const TICK_MS: Record<CountdownKind, number> = {
|
||||||
|
otp: 1000,
|
||||||
|
promotion: 60_000,
|
||||||
|
remaining: 60_000,
|
||||||
|
};
|
||||||
|
|
||||||
|
/** Every icon in a countdown is 16px with a 2px stroke. */
|
||||||
|
export const COUNTDOWN_ICON = { size: 16, strokeWidth: 2 } as const;
|
||||||
|
|
@ -0,0 +1,8 @@
|
||||||
|
export { Countdown } from './Countdown';
|
||||||
|
export { countdownTone } from './tone';
|
||||||
|
export type {
|
||||||
|
CountdownKind,
|
||||||
|
CountdownLabels,
|
||||||
|
CountdownProps,
|
||||||
|
CountdownTone,
|
||||||
|
} from './types';
|
||||||
|
|
@ -0,0 +1,112 @@
|
||||||
|
import { clock, date } from '@lumina/tools/format';
|
||||||
|
import type { ReactNode } from 'react';
|
||||||
|
|
||||||
|
import { Element } from '../../primitives/element';
|
||||||
|
import type { IconName } from '../icon';
|
||||||
|
import {
|
||||||
|
DAY_SECONDS,
|
||||||
|
HOUR_SECONDS,
|
||||||
|
PROMOTION_URGENT_HOURS,
|
||||||
|
REMAINING_WARNING_MIN_DAYS,
|
||||||
|
} from './constants';
|
||||||
|
import { countdownTone } from './tone';
|
||||||
|
import type { CountdownLabels, CountdownProps, CountdownTone } from './types';
|
||||||
|
|
||||||
|
export type CountdownState = {
|
||||||
|
tone: CountdownTone;
|
||||||
|
icon?: IconName;
|
||||||
|
text: ReactNode;
|
||||||
|
/** The OTP clock is still running: `role="timer"`. */
|
||||||
|
isTimer: boolean;
|
||||||
|
};
|
||||||
|
|
||||||
|
type StateInput = Pick<CountdownProps, 'kind' | 'prefix'> & {
|
||||||
|
labels: CountdownLabels;
|
||||||
|
/** Seconds left; `null` while a live clock doesn't know the time yet. */
|
||||||
|
left: number | null;
|
||||||
|
/** The end as epoch ms, for the promotion date. */
|
||||||
|
end: number | null;
|
||||||
|
};
|
||||||
|
|
||||||
|
/** The prefix stays RTL, outside the clock's LTR isolate. */
|
||||||
|
function otpClock(prefix: ReactNode, seconds: number): ReactNode {
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
{prefix}
|
||||||
|
<Element tag="bdi" dir="ltr">
|
||||||
|
{clock(seconds)}
|
||||||
|
</Element>
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The brief's wording and escalation for a given time left, shared by the server render and the
|
||||||
|
* live clock. Returns `null` when nothing should show (a promotion that ended, a missing label).
|
||||||
|
*/
|
||||||
|
export function countdownState({
|
||||||
|
kind,
|
||||||
|
prefix,
|
||||||
|
labels,
|
||||||
|
left,
|
||||||
|
end,
|
||||||
|
}: StateInput): CountdownState | null {
|
||||||
|
if (kind === 'otp') {
|
||||||
|
// Unknown time: the clock's own width, so nothing moves when it fills in.
|
||||||
|
if (left == null)
|
||||||
|
return {
|
||||||
|
tone: 'calm',
|
||||||
|
icon: 'clock',
|
||||||
|
text: otpClock(prefix, 0),
|
||||||
|
isTimer: false,
|
||||||
|
};
|
||||||
|
if (left === 0)
|
||||||
|
return labels.expired == null
|
||||||
|
? null
|
||||||
|
: { tone: 'danger', text: labels.expired, isTimer: false };
|
||||||
|
return {
|
||||||
|
tone: countdownTone(kind, left),
|
||||||
|
icon: 'clock',
|
||||||
|
text: otpClock(prefix, left),
|
||||||
|
isTimer: true,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
if (left == null) return null;
|
||||||
|
|
||||||
|
let state: Omit<CountdownState, 'isTimer'>;
|
||||||
|
if (kind === 'promotion') {
|
||||||
|
const hours = left / HOUR_SECONDS;
|
||||||
|
if (end == null || hours <= 0) return null;
|
||||||
|
state =
|
||||||
|
hours > PROMOTION_URGENT_HOURS
|
||||||
|
? { tone: 'calm', text: labels.until?.(date(end, { time: false })) }
|
||||||
|
: {
|
||||||
|
tone: 'warning',
|
||||||
|
icon: 'clock',
|
||||||
|
text:
|
||||||
|
hours < 1
|
||||||
|
? labels.underAnHour
|
||||||
|
: labels.hoursLeft?.(Math.floor(hours)),
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
const days = left / DAY_SECONDS;
|
||||||
|
const tone = countdownTone(kind, left);
|
||||||
|
state =
|
||||||
|
tone === 'calm'
|
||||||
|
? { tone, text: labels.daysLeft?.(Math.floor(days)) }
|
||||||
|
: tone === 'warning'
|
||||||
|
? {
|
||||||
|
tone,
|
||||||
|
text: labels.daysLeft?.(
|
||||||
|
Math.max(REMAINING_WARNING_MIN_DAYS, Math.floor(days)),
|
||||||
|
),
|
||||||
|
}
|
||||||
|
: // Partial days round up: 2.1 days reads «کمتر از ۳ روز مانده»; past the end, 1.
|
||||||
|
{
|
||||||
|
tone,
|
||||||
|
icon: 'alert',
|
||||||
|
text: labels.underDays?.(Math.max(1, Math.ceil(days))),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
return state.text == null ? null : { ...state, isTimer: false };
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,26 @@
|
||||||
|
import {
|
||||||
|
DAY_SECONDS,
|
||||||
|
HOUR_SECONDS,
|
||||||
|
OTP_URGENT_SECONDS,
|
||||||
|
PROMOTION_URGENT_HOURS,
|
||||||
|
REMAINING_DANGER_DAYS,
|
||||||
|
REMAINING_WARNING_DAYS,
|
||||||
|
} from './constants';
|
||||||
|
import type { CountdownKind, CountdownTone } from './types';
|
||||||
|
|
||||||
|
/** The brief's escalation: calm while there is time, amber when it is short, red when it is urgent. */
|
||||||
|
export function countdownTone(
|
||||||
|
kind: CountdownKind,
|
||||||
|
secondsLeft: number,
|
||||||
|
): CountdownTone {
|
||||||
|
if (kind === 'otp')
|
||||||
|
return secondsLeft <= OTP_URGENT_SECONDS ? 'danger' : 'calm';
|
||||||
|
if (kind === 'promotion') {
|
||||||
|
return secondsLeft / HOUR_SECONDS <= PROMOTION_URGENT_HOURS
|
||||||
|
? 'warning'
|
||||||
|
: 'calm';
|
||||||
|
}
|
||||||
|
const days = secondsLeft / DAY_SECONDS;
|
||||||
|
if (days > REMAINING_WARNING_DAYS) return 'calm';
|
||||||
|
return days > REMAINING_DANGER_DAYS ? 'warning' : 'danger';
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,40 @@
|
||||||
|
import type { DateInput } from '@lumina/tools/format';
|
||||||
|
import type { ReactNode } from 'react';
|
||||||
|
|
||||||
|
/** otp: the code's mm:ss clock. promotion: a promotion's end. remaining: a subscription's time left. */
|
||||||
|
export type CountdownKind = 'otp' | 'promotion' | 'remaining';
|
||||||
|
|
||||||
|
/** calm: time enough. warning: running short. danger: urgent, or the code expired. */
|
||||||
|
export type CountdownTone = 'calm' | 'warning' | 'danger';
|
||||||
|
|
||||||
|
/** Every word around the time, from the translations. Numbers arrive raw; format them there. */
|
||||||
|
export type CountdownLabels = {
|
||||||
|
/** otp at zero: «کد منقضی شد». Omit it to show nothing. */
|
||||||
|
expired?: ReactNode;
|
||||||
|
/** promotion over 48 h, given the formatted end date: «تا ۲۱ مهر ۱۴۰۵». */
|
||||||
|
until?: (date: string) => ReactNode;
|
||||||
|
/** promotion at 48 h or less: «n ساعت باقی مانده» (whole hours, rounded down). */
|
||||||
|
hoursLeft?: (hours: number) => ReactNode;
|
||||||
|
/** promotion under 1 h: «کمتر از ۱ ساعت باقی مانده». */
|
||||||
|
underAnHour?: ReactNode;
|
||||||
|
/** remaining over 3 days: «n روز مانده». */
|
||||||
|
daysLeft?: (days: number) => ReactNode;
|
||||||
|
/** remaining 3 days or less: «کمتر از n روز مانده». */
|
||||||
|
underDays?: (days: number) => ReactNode;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type CountdownProps = {
|
||||||
|
kind: CountdownKind;
|
||||||
|
/** When it ends (otp, promotion; remaining may use it instead of days). */
|
||||||
|
endsAt?: DateInput;
|
||||||
|
/** remaining: days left, fractional allowed. Needs no clock, so it renders on the server. */
|
||||||
|
days?: number;
|
||||||
|
/** Freezes the clock (previews, tests). Also renders the time on the server. */
|
||||||
|
now?: DateInput;
|
||||||
|
labels?: CountdownLabels;
|
||||||
|
/** otp: text before the clock, outside the LTR isolate. */
|
||||||
|
prefix?: ReactNode;
|
||||||
|
/** otp only: fires once at zero. */
|
||||||
|
onExpire?: () => void;
|
||||||
|
className?: string;
|
||||||
|
};
|
||||||
|
|
@ -0,0 +1,13 @@
|
||||||
|
import { cva } from 'class-variance-authority';
|
||||||
|
|
||||||
|
/** The colour sits on the root, so the clock and alert icons take it too. */
|
||||||
|
export const countdownVariants = cva('', {
|
||||||
|
variants: {
|
||||||
|
tone: {
|
||||||
|
calm: 'text-text-secondary',
|
||||||
|
warning: 'text-warning-text',
|
||||||
|
danger: 'text-danger-text',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
defaultVariants: { tone: 'calm' },
|
||||||
|
});
|
||||||
|
|
@ -0,0 +1,48 @@
|
||||||
|
import type { ElementType, ReactElement } from 'react';
|
||||||
|
|
||||||
|
import { Element } from '../../primitives/element';
|
||||||
|
import type { PolymorphicProps } from '../../primitives/element';
|
||||||
|
import { cn } from '../../tools/cn';
|
||||||
|
import type { FlexBoxOwnProps } from './types';
|
||||||
|
|
||||||
|
export type FlexBoxProps<T extends ElementType = 'div'> = PolymorphicProps<
|
||||||
|
T,
|
||||||
|
FlexBoxOwnProps
|
||||||
|
>;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Every flex container. Spacing is `gap`, not `space-*`: it needs no reverse classes, survives
|
||||||
|
* wrapping and conditional children, and runs the same in RTL.
|
||||||
|
*/
|
||||||
|
export function FlexBox<T extends ElementType = 'div'>({
|
||||||
|
display = 'flex',
|
||||||
|
gap,
|
||||||
|
xGap,
|
||||||
|
yGap,
|
||||||
|
wrap,
|
||||||
|
direction,
|
||||||
|
justifyContent,
|
||||||
|
alignItems,
|
||||||
|
alignContent,
|
||||||
|
className,
|
||||||
|
...props
|
||||||
|
}: FlexBoxProps<T>): ReactElement {
|
||||||
|
return (
|
||||||
|
<Element
|
||||||
|
className={cn(
|
||||||
|
display,
|
||||||
|
gap,
|
||||||
|
xGap,
|
||||||
|
yGap,
|
||||||
|
wrap,
|
||||||
|
direction,
|
||||||
|
justifyContent,
|
||||||
|
alignItems,
|
||||||
|
alignContent,
|
||||||
|
className,
|
||||||
|
)}
|
||||||
|
// The generic tag's props can't be checked inside the body; callers are checked above.
|
||||||
|
{...(props as Record<string, unknown>)}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,14 @@
|
||||||
|
export { FlexBox } from './FlexBox';
|
||||||
|
export type { FlexBoxProps } from './FlexBox';
|
||||||
|
export type {
|
||||||
|
AlignContent,
|
||||||
|
AlignItems,
|
||||||
|
FlexBoxOwnProps,
|
||||||
|
FlexDirection,
|
||||||
|
FlexWrap,
|
||||||
|
Gap,
|
||||||
|
JustifyContent,
|
||||||
|
SpaceScale,
|
||||||
|
XGap,
|
||||||
|
YGap,
|
||||||
|
} from './types';
|
||||||
|
|
@ -0,0 +1,54 @@
|
||||||
|
/** The 4px grid: `space-1` … `space-16` of the design system. */
|
||||||
|
export type SpaceScale = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 8 | 10 | 12 | 16;
|
||||||
|
|
||||||
|
export type Gap = `gap-${SpaceScale}`;
|
||||||
|
export type XGap = `gap-x-${SpaceScale}`;
|
||||||
|
export type YGap = `gap-y-${SpaceScale}`;
|
||||||
|
|
||||||
|
export type FlexWrap = 'flex-wrap' | 'flex-wrap-reverse' | 'flex-nowrap';
|
||||||
|
|
||||||
|
export type FlexDirection =
|
||||||
|
| 'flex-row'
|
||||||
|
| 'flex-row-reverse'
|
||||||
|
| 'flex-col'
|
||||||
|
| 'flex-col-reverse';
|
||||||
|
|
||||||
|
export type JustifyContent =
|
||||||
|
| 'justify-normal'
|
||||||
|
| 'justify-start'
|
||||||
|
| 'justify-end'
|
||||||
|
| 'justify-center'
|
||||||
|
| 'justify-between'
|
||||||
|
| 'justify-around'
|
||||||
|
| 'justify-evenly'
|
||||||
|
| 'justify-stretch';
|
||||||
|
|
||||||
|
export type AlignItems =
|
||||||
|
| 'items-start'
|
||||||
|
| 'items-end'
|
||||||
|
| 'items-center'
|
||||||
|
| 'items-baseline'
|
||||||
|
| 'items-stretch';
|
||||||
|
|
||||||
|
export type AlignContent =
|
||||||
|
| 'content-normal'
|
||||||
|
| 'content-start'
|
||||||
|
| 'content-end'
|
||||||
|
| 'content-center'
|
||||||
|
| 'content-between'
|
||||||
|
| 'content-around'
|
||||||
|
| 'content-evenly'
|
||||||
|
| 'content-stretch';
|
||||||
|
|
||||||
|
/** Props take whole class names, so Tailwind finds them in the caller's source. */
|
||||||
|
export type FlexBoxOwnProps = {
|
||||||
|
display?: 'flex' | 'inline-flex';
|
||||||
|
gap?: Gap;
|
||||||
|
xGap?: XGap;
|
||||||
|
yGap?: YGap;
|
||||||
|
wrap?: FlexWrap;
|
||||||
|
direction?: FlexDirection;
|
||||||
|
justifyContent?: JustifyContent;
|
||||||
|
alignItems?: AlignItems;
|
||||||
|
alignContent?: AlignContent;
|
||||||
|
};
|
||||||
|
|
@ -0,0 +1,49 @@
|
||||||
|
import type { ElementType, ReactElement } from 'react';
|
||||||
|
|
||||||
|
import { Element } from '../../primitives/element';
|
||||||
|
import type { PolymorphicProps } from '../../primitives/element';
|
||||||
|
import { cn } from '../../tools/cn';
|
||||||
|
import type { GridBoxOwnProps } from './types';
|
||||||
|
|
||||||
|
export type GridBoxProps<T extends ElementType = 'div'> = PolymorphicProps<
|
||||||
|
T,
|
||||||
|
GridBoxOwnProps
|
||||||
|
>;
|
||||||
|
|
||||||
|
/** Every grid container; the grid counterpart to FlexBox. */
|
||||||
|
export function GridBox<T extends ElementType = 'div'>({
|
||||||
|
display = 'grid',
|
||||||
|
cols,
|
||||||
|
rows,
|
||||||
|
gap,
|
||||||
|
xGap,
|
||||||
|
yGap,
|
||||||
|
flow,
|
||||||
|
justifyItems,
|
||||||
|
justifyContent,
|
||||||
|
alignItems,
|
||||||
|
alignContent,
|
||||||
|
className,
|
||||||
|
...props
|
||||||
|
}: GridBoxProps<T>): ReactElement {
|
||||||
|
return (
|
||||||
|
<Element
|
||||||
|
className={cn(
|
||||||
|
display,
|
||||||
|
cols,
|
||||||
|
rows,
|
||||||
|
gap,
|
||||||
|
xGap,
|
||||||
|
yGap,
|
||||||
|
flow,
|
||||||
|
justifyItems,
|
||||||
|
justifyContent,
|
||||||
|
alignItems,
|
||||||
|
alignContent,
|
||||||
|
className,
|
||||||
|
)}
|
||||||
|
// The generic tag's props can't be checked inside the body; callers are checked above.
|
||||||
|
{...(props as Record<string, unknown>)}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,9 @@
|
||||||
|
export { GridBox } from './GridBox';
|
||||||
|
export type { GridBoxProps } from './GridBox';
|
||||||
|
export type {
|
||||||
|
GridBoxOwnProps,
|
||||||
|
GridCols,
|
||||||
|
GridFlow,
|
||||||
|
GridRows,
|
||||||
|
JustifyItems,
|
||||||
|
} from './types';
|
||||||
|
|
@ -0,0 +1,55 @@
|
||||||
|
import type {
|
||||||
|
AlignContent,
|
||||||
|
AlignItems,
|
||||||
|
Gap,
|
||||||
|
JustifyContent,
|
||||||
|
XGap,
|
||||||
|
YGap,
|
||||||
|
} from '../flex-box';
|
||||||
|
|
||||||
|
export type GridCols = `grid-cols-${
|
||||||
|
| 1
|
||||||
|
| 2
|
||||||
|
| 3
|
||||||
|
| 4
|
||||||
|
| 5
|
||||||
|
| 6
|
||||||
|
| 7
|
||||||
|
| 8
|
||||||
|
| 9
|
||||||
|
| 10
|
||||||
|
| 11
|
||||||
|
| 12
|
||||||
|
| 'none'
|
||||||
|
| 'subgrid'}`;
|
||||||
|
|
||||||
|
export type GridRows =
|
||||||
|
`grid-rows-${1 | 2 | 3 | 4 | 5 | 6 | 'none' | 'subgrid'}`;
|
||||||
|
|
||||||
|
export type GridFlow =
|
||||||
|
| 'grid-flow-row'
|
||||||
|
| 'grid-flow-col'
|
||||||
|
| 'grid-flow-dense'
|
||||||
|
| 'grid-flow-row-dense'
|
||||||
|
| 'grid-flow-col-dense';
|
||||||
|
|
||||||
|
export type JustifyItems =
|
||||||
|
| 'justify-items-start'
|
||||||
|
| 'justify-items-end'
|
||||||
|
| 'justify-items-center'
|
||||||
|
| 'justify-items-stretch';
|
||||||
|
|
||||||
|
/** Props take whole class names, so Tailwind finds them in the caller's source. */
|
||||||
|
export type GridBoxOwnProps = {
|
||||||
|
display?: 'grid' | 'inline-grid';
|
||||||
|
cols?: GridCols;
|
||||||
|
rows?: GridRows;
|
||||||
|
gap?: Gap;
|
||||||
|
xGap?: XGap;
|
||||||
|
yGap?: YGap;
|
||||||
|
flow?: GridFlow;
|
||||||
|
justifyItems?: JustifyItems;
|
||||||
|
justifyContent?: JustifyContent;
|
||||||
|
alignItems?: AlignItems;
|
||||||
|
alignContent?: AlignContent;
|
||||||
|
};
|
||||||
|
|
@ -0,0 +1,36 @@
|
||||||
|
import { Element } from '../../primitives/element';
|
||||||
|
import { cn } from '../../tools/cn';
|
||||||
|
import { SPRITE_URL } from './constants';
|
||||||
|
import type { IconProps } from './types';
|
||||||
|
|
||||||
|
/** A line icon from the sprite, drawn in `currentColor` on a 24px grid. */
|
||||||
|
export function Icon({
|
||||||
|
name,
|
||||||
|
size = 20,
|
||||||
|
strokeWidth = 1.75,
|
||||||
|
label,
|
||||||
|
className,
|
||||||
|
...props
|
||||||
|
}: IconProps) {
|
||||||
|
return (
|
||||||
|
<Element
|
||||||
|
tag="svg"
|
||||||
|
width={size}
|
||||||
|
height={size}
|
||||||
|
viewBox="0 0 24 24"
|
||||||
|
fill="none"
|
||||||
|
stroke="currentColor"
|
||||||
|
strokeWidth={strokeWidth}
|
||||||
|
strokeLinecap="round"
|
||||||
|
strokeLinejoin="round"
|
||||||
|
focusable="false"
|
||||||
|
role={label ? 'img' : undefined}
|
||||||
|
aria-label={label}
|
||||||
|
aria-hidden={label ? undefined : true}
|
||||||
|
className={cn('inline-block flex-none align-middle', className)}
|
||||||
|
{...props}
|
||||||
|
>
|
||||||
|
<Element tag="use" href={`${SPRITE_URL}#${name}`} />
|
||||||
|
</Element>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,7 @@
|
||||||
|
import sprite from './sprite.svg';
|
||||||
|
|
||||||
|
/** Next.js imports a static file as `{ src }`, other bundlers as a URL string. */
|
||||||
|
const asset = sprite as unknown as string | { src: string };
|
||||||
|
|
||||||
|
/** One hashed file the browser caches; each icon is a `<use>` into it, so icons add no JavaScript. */
|
||||||
|
export const SPRITE_URL = typeof asset === 'string' ? asset : asset.src;
|
||||||
|
|
@ -0,0 +1,73 @@
|
||||||
|
// Generated by tools/icon-sprite-plugin from src/icons/*.svg. Run `pnpm nx run island:icons`.
|
||||||
|
|
||||||
|
export const ICON_NAMES = [
|
||||||
|
'alert',
|
||||||
|
'analyst',
|
||||||
|
'arrow-down',
|
||||||
|
'arrow-left',
|
||||||
|
'arrow-right',
|
||||||
|
'arrow-up',
|
||||||
|
'bale',
|
||||||
|
'ban',
|
||||||
|
'bell-off',
|
||||||
|
'bell',
|
||||||
|
'calendar',
|
||||||
|
'channel',
|
||||||
|
'check-circle',
|
||||||
|
'check',
|
||||||
|
'chevron-down',
|
||||||
|
'chevron-left',
|
||||||
|
'chevron-right',
|
||||||
|
'clock',
|
||||||
|
'close',
|
||||||
|
'copy',
|
||||||
|
'dashboard',
|
||||||
|
'download',
|
||||||
|
'edit',
|
||||||
|
'external',
|
||||||
|
'eye',
|
||||||
|
'filter',
|
||||||
|
'flag',
|
||||||
|
'grip',
|
||||||
|
'headset',
|
||||||
|
'home',
|
||||||
|
'image',
|
||||||
|
'inbox',
|
||||||
|
'info',
|
||||||
|
'investor',
|
||||||
|
'link',
|
||||||
|
'lock',
|
||||||
|
'log-out',
|
||||||
|
'message',
|
||||||
|
'minus-circle',
|
||||||
|
'minus',
|
||||||
|
'more',
|
||||||
|
'pause',
|
||||||
|
'plus',
|
||||||
|
'receipt',
|
||||||
|
'refresh',
|
||||||
|
'search-x',
|
||||||
|
'search',
|
||||||
|
'server',
|
||||||
|
'share',
|
||||||
|
'shield-check',
|
||||||
|
'sort',
|
||||||
|
'star',
|
||||||
|
'tag',
|
||||||
|
'target',
|
||||||
|
'telegram',
|
||||||
|
'trash',
|
||||||
|
'trend-down',
|
||||||
|
'trend-up',
|
||||||
|
'upload',
|
||||||
|
'user',
|
||||||
|
'users',
|
||||||
|
'wallet',
|
||||||
|
'wifi-off',
|
||||||
|
'wifi',
|
||||||
|
'x-circle',
|
||||||
|
'zoom-in',
|
||||||
|
'zoom-out',
|
||||||
|
] as const;
|
||||||
|
|
||||||
|
export type IconName = (typeof ICON_NAMES)[number];
|
||||||
|
|
@ -0,0 +1,4 @@
|
||||||
|
export { Icon } from './Icon';
|
||||||
|
export { ICON_NAMES } from './icon-names';
|
||||||
|
export type { IconName } from './icon-names';
|
||||||
|
export type { IconProps } from './types';
|
||||||
|
After Width: | Height: | Size: 8.6 KiB |
|
|
@ -0,0 +1,12 @@
|
||||||
|
import type { SVGProps } from 'react';
|
||||||
|
|
||||||
|
import type { IconName } from './icon-names';
|
||||||
|
|
||||||
|
export type IconProps = Omit<SVGProps<SVGSVGElement>, 'name' | 'children'> & {
|
||||||
|
name: IconName;
|
||||||
|
/** px. 16 in badges, 20 by default, 24 in navigation. */
|
||||||
|
size?: number;
|
||||||
|
strokeWidth?: number;
|
||||||
|
/** Accessible name. Omit it for a decorative icon next to text, which screen readers then skip. */
|
||||||
|
label?: string;
|
||||||
|
};
|
||||||
|
|
@ -0,0 +1,33 @@
|
||||||
|
import NextLink from 'next/link';
|
||||||
|
|
||||||
|
import { cn } from '../../tools/cn';
|
||||||
|
import { FlexBox } from '../flex-box';
|
||||||
|
import { Icon } from '../icon';
|
||||||
|
import { Typography } from '../typography';
|
||||||
|
import type { LinkProps } from './types';
|
||||||
|
import { linkVariants } from './variants';
|
||||||
|
|
||||||
|
/** An inline or standalone text link in `text-link`, underlined on hover. */
|
||||||
|
export function Link({
|
||||||
|
standalone,
|
||||||
|
icon,
|
||||||
|
className,
|
||||||
|
children,
|
||||||
|
...props
|
||||||
|
}: LinkProps) {
|
||||||
|
return (
|
||||||
|
<FlexBox
|
||||||
|
tag={NextLink}
|
||||||
|
display="inline-flex"
|
||||||
|
alignItems="items-center"
|
||||||
|
gap="gap-1"
|
||||||
|
className={cn(linkVariants({ standalone }), className)}
|
||||||
|
{...props}
|
||||||
|
>
|
||||||
|
<Typography tag="span" variant="inherit" color="inherit">
|
||||||
|
{children}
|
||||||
|
</Typography>
|
||||||
|
{icon && <Icon name={icon} size={18} />}
|
||||||
|
</FlexBox>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,3 @@
|
||||||
|
export { Link } from './Link';
|
||||||
|
export type { LinkProps } from './types';
|
||||||
|
export { linkVariants } from './variants';
|
||||||
|
|
@ -0,0 +1,12 @@
|
||||||
|
import type { VariantProps } from 'class-variance-authority';
|
||||||
|
import type NextLink from 'next/link';
|
||||||
|
import type { ComponentPropsWithRef } from 'react';
|
||||||
|
|
||||||
|
import type { IconName } from '../icon';
|
||||||
|
import type { linkVariants } from './variants';
|
||||||
|
|
||||||
|
export type LinkProps = ComponentPropsWithRef<typeof NextLink> &
|
||||||
|
VariantProps<typeof linkVariants> & {
|
||||||
|
/** Trailing icon, such as `external` or `chevron-left` ("go to" points left in RTL). */
|
||||||
|
icon?: IconName;
|
||||||
|
};
|
||||||
|
|
@ -0,0 +1,12 @@
|
||||||
|
import { cva } from 'class-variance-authority';
|
||||||
|
|
||||||
|
export const linkVariants = cva(
|
||||||
|
'cursor-pointer rounded-xs font-semibold text-text-link underline decoration-transparent decoration-[1.5px] underline-offset-5 transition-[text-decoration-color] duration-120 ease-standard hover:decoration-current focus-visible:focus-ring',
|
||||||
|
{
|
||||||
|
variants: {
|
||||||
|
/** A link on its own line, not inside a sentence, gets a 44px hit area. */
|
||||||
|
standalone: { true: 'min-h-touch-target', false: '' },
|
||||||
|
},
|
||||||
|
defaultVariants: { standalone: false },
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
@ -0,0 +1,20 @@
|
||||||
|
import { Element } from '../../primitives/element';
|
||||||
|
import { cn } from '../../tools/cn';
|
||||||
|
import type { SeparatorProps } from './types';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A hairline between inline items, such as «مرحله ۲ از ۵ | شماره موبایل». Never a middle dot:
|
||||||
|
* next to Persian digits it reads as a zero.
|
||||||
|
*/
|
||||||
|
export function Separator({ className }: SeparatorProps) {
|
||||||
|
return (
|
||||||
|
<Element
|
||||||
|
tag="span"
|
||||||
|
aria-hidden
|
||||||
|
className={cn(
|
||||||
|
'mx-1 inline-block h-3 w-px flex-none bg-border-default align-middle opacity-60',
|
||||||
|
className,
|
||||||
|
)}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,2 @@
|
||||||
|
export { Separator } from './Separator';
|
||||||
|
export type { SeparatorProps } from './types';
|
||||||
|
|
@ -0,0 +1,3 @@
|
||||||
|
export type SeparatorProps = {
|
||||||
|
className?: string;
|
||||||
|
};
|
||||||
|
|
@ -0,0 +1,54 @@
|
||||||
|
import { Element } from '../../primitives/element';
|
||||||
|
import { cn } from '../../tools/cn';
|
||||||
|
import { FlexBox } from '../flex-box';
|
||||||
|
import { LAST_LINE_WIDTH } from './constants';
|
||||||
|
import type { SkeletonProps } from './types';
|
||||||
|
import { skeletonVariants } from './variants';
|
||||||
|
|
||||||
|
/** One placeholder block, hidden from screen readers. Group blocks in a SkeletonRegion. */
|
||||||
|
export function Skeleton({
|
||||||
|
shape = 'text',
|
||||||
|
width,
|
||||||
|
height,
|
||||||
|
lines = 1,
|
||||||
|
className,
|
||||||
|
style,
|
||||||
|
...props
|
||||||
|
}: SkeletonProps) {
|
||||||
|
if (shape === 'text' && lines > 1) {
|
||||||
|
// The wrapper takes the caller's class and width; each line keeps the plain text block.
|
||||||
|
return (
|
||||||
|
<FlexBox
|
||||||
|
tag="span"
|
||||||
|
direction="flex-col"
|
||||||
|
aria-hidden
|
||||||
|
className={cn('gap-2.5', className)}
|
||||||
|
style={{ width, ...style }}
|
||||||
|
{...props}
|
||||||
|
>
|
||||||
|
{Array.from({ length: lines }, (_, line) => (
|
||||||
|
<Element
|
||||||
|
tag="span"
|
||||||
|
// Static placeholders that never reorder, so the index is a stable key.
|
||||||
|
key={line}
|
||||||
|
className={skeletonVariants({ shape })}
|
||||||
|
style={{
|
||||||
|
height,
|
||||||
|
width: line === lines - 1 ? LAST_LINE_WIDTH : '100%',
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
))}
|
||||||
|
</FlexBox>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Element
|
||||||
|
tag="span"
|
||||||
|
aria-hidden
|
||||||
|
className={cn(skeletonVariants({ shape }), className)}
|
||||||
|
style={{ width, height, ...style }}
|
||||||
|
{...props}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,32 @@
|
||||||
|
import { cn } from '../../tools/cn';
|
||||||
|
import { FlexBox } from '../flex-box';
|
||||||
|
import { Typography } from '../typography';
|
||||||
|
import type { SkeletonRegionProps } from './types';
|
||||||
|
import { skeletonRegionVariants } from './variants';
|
||||||
|
|
||||||
|
/** Wraps a skeleton layout: announced once as busy, with its blocks hidden. */
|
||||||
|
export function SkeletonRegion({
|
||||||
|
label,
|
||||||
|
delayed,
|
||||||
|
className,
|
||||||
|
children,
|
||||||
|
...props
|
||||||
|
}: SkeletonRegionProps) {
|
||||||
|
return (
|
||||||
|
<FlexBox
|
||||||
|
direction="flex-col"
|
||||||
|
gap="gap-3"
|
||||||
|
// Not <output>: presets put block layouts inside, and <output> holds phrasing content only.
|
||||||
|
// oxlint-disable-next-line jsx-a11y/prefer-tag-over-role
|
||||||
|
role="status"
|
||||||
|
aria-busy
|
||||||
|
className={cn(skeletonRegionVariants({ delayed }), className)}
|
||||||
|
{...props}
|
||||||
|
>
|
||||||
|
<Typography tag="span" className="sr-only">
|
||||||
|
{label}
|
||||||
|
</Typography>
|
||||||
|
{children}
|
||||||
|
</FlexBox>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,2 @@
|
||||||
|
/** The last of several text lines stops short, as a paragraph's last line does. */
|
||||||
|
export const LAST_LINE_WIDTH = '62%';
|
||||||
|
|
@ -0,0 +1,4 @@
|
||||||
|
export { Skeleton } from './Skeleton';
|
||||||
|
export { SkeletonRegion } from './SkeletonRegion';
|
||||||
|
export type { SkeletonProps, SkeletonRegionProps } from './types';
|
||||||
|
export { skeletonRegionVariants, skeletonVariants } from './variants';
|
||||||
|
|
@ -0,0 +1,22 @@
|
||||||
|
import type { VariantProps } from 'class-variance-authority';
|
||||||
|
import type { ComponentPropsWithRef, CSSProperties, ReactNode } from 'react';
|
||||||
|
|
||||||
|
import type { skeletonRegionVariants, skeletonVariants } from './variants';
|
||||||
|
|
||||||
|
export type SkeletonProps = Omit<ComponentPropsWithRef<'span'>, 'children'> &
|
||||||
|
VariantProps<typeof skeletonVariants> & {
|
||||||
|
width?: CSSProperties['width'];
|
||||||
|
height?: CSSProperties['height'];
|
||||||
|
/**
|
||||||
|
* `text` only: n lines, the last one at 62%. `className`, `style` and `width` then go on the
|
||||||
|
* lines' wrapper; `height` on each line.
|
||||||
|
*/
|
||||||
|
lines?: number;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type SkeletonRegionProps = Omit<ComponentPropsWithRef<'div'>, 'role'> &
|
||||||
|
VariantProps<typeof skeletonRegionVariants> & {
|
||||||
|
/** Hidden status text read by screen readers, such as «در حال بارگذاری…». */
|
||||||
|
label: string;
|
||||||
|
children: ReactNode;
|
||||||
|
};
|
||||||
|
|
@ -0,0 +1,27 @@
|
||||||
|
import { cva } from 'class-variance-authority';
|
||||||
|
|
||||||
|
export const skeletonVariants = cva(
|
||||||
|
'block h-3 max-w-full flex-none animate-shimmer bg-skeleton bg-linear-90 from-transparent via-skeleton-highlight to-transparent bg-size-[200%_100%] bg-no-repeat motion-reduce:animate-none motion-reduce:bg-none',
|
||||||
|
{
|
||||||
|
variants: {
|
||||||
|
shape: {
|
||||||
|
text: 'rounded-xs',
|
||||||
|
rect: 'rounded-md',
|
||||||
|
circle: 'rounded-full',
|
||||||
|
pill: 'rounded-full',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
defaultVariants: { shape: 'text' },
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
export const skeletonRegionVariants = cva('', {
|
||||||
|
variants: {
|
||||||
|
/** Fades in after 300ms, so a fast response never flashes a skeleton. Set it in loading.tsx. */
|
||||||
|
delayed: {
|
||||||
|
true: 'animate-appear-delayed motion-reduce:animate-none',
|
||||||
|
false: '',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
defaultVariants: { delayed: false },
|
||||||
|
});
|
||||||
|
|
@ -0,0 +1,18 @@
|
||||||
|
import { Element } from '../../primitives/element';
|
||||||
|
import { cn } from '../../tools/cn';
|
||||||
|
import type { SpinnerProps } from './types';
|
||||||
|
|
||||||
|
/** A ring that turns while one action is in flight. Never for loading a page: use Skeleton. */
|
||||||
|
export function Spinner({ className, ...props }: SpinnerProps) {
|
||||||
|
return (
|
||||||
|
<Element
|
||||||
|
tag="span"
|
||||||
|
aria-hidden
|
||||||
|
className={cn(
|
||||||
|
'inline-block size-[1.125rem] flex-none animate-spin rounded-full border-2 border-current border-e-transparent',
|
||||||
|
className,
|
||||||
|
)}
|
||||||
|
{...props}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,2 @@
|
||||||
|
export { Spinner } from './Spinner';
|
||||||
|
export type { SpinnerProps } from './types';
|
||||||
|
|
@ -0,0 +1,3 @@
|
||||||
|
import type { ComponentPropsWithRef } from 'react';
|
||||||
|
|
||||||
|
export type SpinnerProps = Omit<ComponentPropsWithRef<'span'>, 'children'>;
|
||||||
|
|
@ -0,0 +1,56 @@
|
||||||
|
import { Element } from '../../primitives/element';
|
||||||
|
import { cn } from '../../tools/cn';
|
||||||
|
import { GridBox } from '../grid-box';
|
||||||
|
import { Icon } from '../icon';
|
||||||
|
import { STATE_ICON_SIZES } from './constants';
|
||||||
|
import type { StateArtProps } from './types';
|
||||||
|
import {
|
||||||
|
stateArtVariants,
|
||||||
|
stateDiscVariants,
|
||||||
|
stateDotVariants,
|
||||||
|
} from './variants';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The shared art of empty and error states: an icon in a disc of the tone's subtle ground, a
|
||||||
|
* dashed ring, and an accent dot at the inline start. Decorative; the title says what happened.
|
||||||
|
*/
|
||||||
|
export function StateArt({
|
||||||
|
icon,
|
||||||
|
tone = 'brand',
|
||||||
|
size = 'regular',
|
||||||
|
className,
|
||||||
|
}: StateArtProps) {
|
||||||
|
const disc = (
|
||||||
|
<GridBox
|
||||||
|
tag="span"
|
||||||
|
justifyItems="justify-items-center"
|
||||||
|
alignItems="items-center"
|
||||||
|
aria-hidden
|
||||||
|
className={cn(
|
||||||
|
stateDiscVariants({ tone, size }),
|
||||||
|
size === 'inline' && className,
|
||||||
|
)}
|
||||||
|
>
|
||||||
|
<Icon name={icon} size={STATE_ICON_SIZES[size]} />
|
||||||
|
</GridBox>
|
||||||
|
);
|
||||||
|
|
||||||
|
if (size === 'inline') return disc;
|
||||||
|
|
||||||
|
return (
|
||||||
|
<GridBox
|
||||||
|
tag="span"
|
||||||
|
justifyItems="justify-items-center"
|
||||||
|
alignItems="items-center"
|
||||||
|
aria-hidden
|
||||||
|
className={cn(stateArtVariants({ size }), className)}
|
||||||
|
>
|
||||||
|
<Element
|
||||||
|
tag="span"
|
||||||
|
className="absolute inset-0 rounded-full border-[1.5px] border-dashed border-border-strong"
|
||||||
|
/>
|
||||||
|
<Element tag="span" className={stateDotVariants({ tone, size })} />
|
||||||
|
{disc}
|
||||||
|
</GridBox>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,10 @@
|
||||||
|
import type { StateArtProps } from './types';
|
||||||
|
|
||||||
|
export const STATE_ICON_SIZES: Record<
|
||||||
|
NonNullable<StateArtProps['size']>,
|
||||||
|
number
|
||||||
|
> = {
|
||||||
|
regular: 30,
|
||||||
|
compact: 22,
|
||||||
|
inline: 20,
|
||||||
|
};
|
||||||
|
|
@ -0,0 +1,2 @@
|
||||||
|
export { StateArt } from './StateArt';
|
||||||
|
export type { StateArtProps, StateTone } from './types';
|
||||||
|
|
@ -0,0 +1,11 @@
|
||||||
|
import type { IconName } from '../icon';
|
||||||
|
|
||||||
|
export type StateTone = 'brand' | 'neutral' | 'success' | 'danger' | 'warning';
|
||||||
|
|
||||||
|
export type StateArtProps = {
|
||||||
|
icon: IconName;
|
||||||
|
tone?: StateTone;
|
||||||
|
/** `inline` draws only the 40px disc, for one-row states. */
|
||||||
|
size?: 'regular' | 'compact' | 'inline';
|
||||||
|
className?: string;
|
||||||
|
};
|
||||||
|
|
@ -0,0 +1,60 @@
|
||||||
|
import { cva } from 'class-variance-authority';
|
||||||
|
|
||||||
|
export const stateArtVariants = cva('relative flex-none', {
|
||||||
|
variants: {
|
||||||
|
size: {
|
||||||
|
regular: 'size-26',
|
||||||
|
compact: 'size-19',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
defaultVariants: { size: 'regular' },
|
||||||
|
});
|
||||||
|
|
||||||
|
/** The disc holding the icon, in the tone's subtle ground. */
|
||||||
|
export const stateDiscVariants = cva('rounded-full', {
|
||||||
|
variants: {
|
||||||
|
tone: {
|
||||||
|
brand: 'bg-action-primary-subtle text-action-primary',
|
||||||
|
neutral: 'bg-surface-sunken text-text-secondary',
|
||||||
|
success: 'bg-success-subtle text-success-text',
|
||||||
|
danger: 'bg-danger-subtle text-danger',
|
||||||
|
warning: 'bg-warning-subtle text-warning-text',
|
||||||
|
},
|
||||||
|
size: {
|
||||||
|
regular: 'size-18',
|
||||||
|
compact: 'size-13',
|
||||||
|
/** A row's leading icon, with no ring or dot. */
|
||||||
|
inline: 'size-10 flex-none',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
compoundVariants: [
|
||||||
|
// An inline brand icon reads as neutral; brand is for first-use art only.
|
||||||
|
{
|
||||||
|
size: 'inline',
|
||||||
|
tone: 'brand',
|
||||||
|
className: 'bg-surface-sunken text-text-secondary',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
defaultVariants: { tone: 'brand', size: 'regular' },
|
||||||
|
});
|
||||||
|
|
||||||
|
/** The accent dot at the inline start, cut out of the ring by a surface halo. */
|
||||||
|
export const stateDotVariants = cva(
|
||||||
|
'absolute rounded-full shadow-[0_0_0_4px_var(--surface)]',
|
||||||
|
{
|
||||||
|
variants: {
|
||||||
|
tone: {
|
||||||
|
brand: 'bg-action-primary',
|
||||||
|
neutral: 'bg-neutral-mark',
|
||||||
|
success: 'bg-success',
|
||||||
|
danger: 'bg-danger',
|
||||||
|
warning: 'bg-warning',
|
||||||
|
},
|
||||||
|
size: {
|
||||||
|
regular: 'start-2.5 top-2.5 size-2.5',
|
||||||
|
compact: 'start-1.5 top-1.5 size-2',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
defaultVariants: { tone: 'brand', size: 'regular' },
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
@ -0,0 +1,44 @@
|
||||||
|
import { cn } from '../../tools/cn';
|
||||||
|
import { FlexBox } from '../flex-box';
|
||||||
|
import { Icon } from '../icon';
|
||||||
|
import { Typography } from '../typography';
|
||||||
|
import { STATUS_STYLES, TONE_ICONS } from './constants';
|
||||||
|
import type { StatusBadgeProps } from './types';
|
||||||
|
import { statusBadgeVariants } from './variants';
|
||||||
|
|
||||||
|
/** A state as a word and an icon on its tone, so a state always reads the same. */
|
||||||
|
export function StatusBadge({
|
||||||
|
status,
|
||||||
|
tone: toneProp,
|
||||||
|
icon: iconProp,
|
||||||
|
plain,
|
||||||
|
className,
|
||||||
|
children,
|
||||||
|
...props
|
||||||
|
}: StatusBadgeProps) {
|
||||||
|
const style = status ? STATUS_STYLES[status] : undefined;
|
||||||
|
const tone = toneProp ?? style?.tone ?? 'neutral';
|
||||||
|
const icon =
|
||||||
|
iconProp === undefined ? (style ? style.icon : TONE_ICONS[tone]) : iconProp;
|
||||||
|
|
||||||
|
return (
|
||||||
|
<FlexBox
|
||||||
|
tag="span"
|
||||||
|
display="inline-flex"
|
||||||
|
alignItems="items-center"
|
||||||
|
gap="gap-1"
|
||||||
|
className={cn(statusBadgeVariants({ tone, plain }), className)}
|
||||||
|
{...props}
|
||||||
|
>
|
||||||
|
{icon && <Icon name={icon} size={16} strokeWidth={2} />}
|
||||||
|
<Typography
|
||||||
|
tag="span"
|
||||||
|
variant="caption"
|
||||||
|
color="inherit"
|
||||||
|
className="font-semibold"
|
||||||
|
>
|
||||||
|
{children}
|
||||||
|
</Typography>
|
||||||
|
</FlexBox>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,32 @@
|
||||||
|
import type { IconName } from '../icon';
|
||||||
|
import type { StatusKey, StatusStyle, StatusTone } from './types';
|
||||||
|
|
||||||
|
export const STATUS_STYLES: Record<StatusKey, StatusStyle> = {
|
||||||
|
'verification.pending': { tone: 'neutral', icon: 'clock' },
|
||||||
|
'verification.approved': { tone: 'brand', icon: 'check' },
|
||||||
|
'verification.rejected': { tone: 'danger', icon: 'alert' },
|
||||||
|
'subscription.active': { tone: 'success', icon: 'check' },
|
||||||
|
'subscription.expired': { tone: 'neutral', icon: null },
|
||||||
|
'subscription.suspended': { tone: 'warning', icon: 'pause' },
|
||||||
|
'subscription.refunded': { tone: 'neutral', icon: 'receipt' },
|
||||||
|
'payment.successful': { tone: 'success', icon: 'check' },
|
||||||
|
'payment.failed': { tone: 'danger', icon: 'x-circle' },
|
||||||
|
'payment.pending': { tone: 'warning', icon: 'clock' },
|
||||||
|
'payout.in_progress': { tone: 'neutral', icon: 'clock' },
|
||||||
|
'payout.cancelled': { tone: 'danger', icon: 'x-circle' },
|
||||||
|
'account.active': { tone: 'success', icon: 'check' },
|
||||||
|
'account.suspended': { tone: 'danger', icon: 'ban' },
|
||||||
|
'prediction.open': { tone: 'brand', icon: 'clock' },
|
||||||
|
'outcome.win': { tone: 'success', icon: 'check-circle' },
|
||||||
|
'outcome.loss': { tone: 'danger', icon: 'x-circle' },
|
||||||
|
'outcome.breakeven': { tone: 'neutral', icon: 'minus-circle' },
|
||||||
|
};
|
||||||
|
|
||||||
|
/** The icon of a badge that has a tone but no status. */
|
||||||
|
export const TONE_ICONS: Record<StatusTone, IconName> = {
|
||||||
|
brand: 'check',
|
||||||
|
success: 'check',
|
||||||
|
neutral: 'clock',
|
||||||
|
danger: 'alert',
|
||||||
|
warning: 'clock',
|
||||||
|
};
|
||||||
|
|
@ -0,0 +1,4 @@
|
||||||
|
export { StatusBadge } from './StatusBadge';
|
||||||
|
export { STATUS_STYLES } from './constants';
|
||||||
|
export type { StatusBadgeProps, StatusKey, StatusTone } from './types';
|
||||||
|
export { statusBadgeVariants } from './variants';
|
||||||
|
|
@ -0,0 +1,44 @@
|
||||||
|
import type { VariantProps } from 'class-variance-authority';
|
||||||
|
import type { ComponentPropsWithRef, ReactNode } from 'react';
|
||||||
|
|
||||||
|
import type { IconName } from '../icon';
|
||||||
|
import type { statusBadgeVariants } from './variants';
|
||||||
|
|
||||||
|
export type StatusTone = NonNullable<
|
||||||
|
VariantProps<typeof statusBadgeVariants>['tone']
|
||||||
|
>;
|
||||||
|
|
||||||
|
/** The status vocabulary: each key always reads with the same tone and icon. */
|
||||||
|
export type StatusKey =
|
||||||
|
| 'verification.pending'
|
||||||
|
| 'verification.approved'
|
||||||
|
| 'verification.rejected'
|
||||||
|
| 'subscription.active'
|
||||||
|
| 'subscription.expired'
|
||||||
|
| 'subscription.suspended'
|
||||||
|
| 'subscription.refunded'
|
||||||
|
| 'payment.successful'
|
||||||
|
| 'payment.failed'
|
||||||
|
| 'payment.pending'
|
||||||
|
| 'payout.in_progress'
|
||||||
|
| 'payout.cancelled'
|
||||||
|
| 'account.active'
|
||||||
|
| 'account.suspended'
|
||||||
|
| 'prediction.open'
|
||||||
|
| 'outcome.win'
|
||||||
|
| 'outcome.loss'
|
||||||
|
| 'outcome.breakeven';
|
||||||
|
|
||||||
|
export type StatusStyle = { tone: StatusTone; icon: IconName | null };
|
||||||
|
|
||||||
|
export type StatusBadgeProps = Omit<ComponentPropsWithRef<'span'>, 'children'> &
|
||||||
|
Pick<VariantProps<typeof statusBadgeVariants>, 'plain'> & {
|
||||||
|
/** Sets tone and icon together; the label comes from the same key in the translations. */
|
||||||
|
status?: StatusKey;
|
||||||
|
/** Overrides the status's tone, or sets it for a badge without a status. */
|
||||||
|
tone?: StatusTone;
|
||||||
|
/** `null` hides the icon. */
|
||||||
|
icon?: IconName | null;
|
||||||
|
/** The label. Status always pairs a word with an icon, never colour alone. */
|
||||||
|
children: ReactNode;
|
||||||
|
};
|
||||||
|
|
@ -0,0 +1,19 @@
|
||||||
|
import { cva } from 'class-variance-authority';
|
||||||
|
|
||||||
|
export const statusBadgeVariants = cva('whitespace-nowrap', {
|
||||||
|
variants: {
|
||||||
|
tone: {
|
||||||
|
brand: 'bg-action-primary-subtle text-action-primary-text',
|
||||||
|
success: 'bg-success-subtle text-success-text',
|
||||||
|
neutral: 'bg-surface-sunken text-text-secondary',
|
||||||
|
danger: 'bg-danger-subtle text-danger-text',
|
||||||
|
warning: 'bg-warning-subtle text-warning-text',
|
||||||
|
},
|
||||||
|
/** No pill: text and icon only, such as a completed payout. */
|
||||||
|
plain: {
|
||||||
|
true: 'bg-transparent',
|
||||||
|
false: 'h-6 rounded-sm px-2',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
defaultVariants: { tone: 'neutral', plain: false },
|
||||||
|
});
|
||||||
|
|
@ -0,0 +1,39 @@
|
||||||
|
import { cn } from '../../tools/cn';
|
||||||
|
import { FlexBox } from '../flex-box';
|
||||||
|
import { Icon } from '../icon';
|
||||||
|
import { linkVariants } from '../link';
|
||||||
|
import { Typography } from '../typography';
|
||||||
|
import type { SupportLinkProps } from './types';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* «تماس با پشتیبانی»: looks like a Link but is a button, because it opens SupportSheet rather
|
||||||
|
* than navigating. Inside a dimmed card, put it in the card's `action` so it stays live.
|
||||||
|
*/
|
||||||
|
export function SupportLink({
|
||||||
|
icon = true,
|
||||||
|
standalone,
|
||||||
|
className,
|
||||||
|
children,
|
||||||
|
...props
|
||||||
|
}: SupportLinkProps) {
|
||||||
|
return (
|
||||||
|
<FlexBox
|
||||||
|
tag="button"
|
||||||
|
type="button"
|
||||||
|
display="inline-flex"
|
||||||
|
alignItems="items-center"
|
||||||
|
gap="gap-1"
|
||||||
|
className={cn(
|
||||||
|
linkVariants({ standalone }),
|
||||||
|
'border-0 bg-transparent p-0 text-start',
|
||||||
|
className,
|
||||||
|
)}
|
||||||
|
{...props}
|
||||||
|
>
|
||||||
|
{icon && <Icon name="headset" size={18} />}
|
||||||
|
<Typography tag="span" variant="inherit" color="inherit">
|
||||||
|
{children}
|
||||||
|
</Typography>
|
||||||
|
</FlexBox>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,2 @@
|
||||||
|
export { SupportLink } from './SupportLink';
|
||||||
|
export type { SupportLinkProps } from './types';
|
||||||
|
|
@ -0,0 +1,13 @@
|
||||||
|
import type { ComponentPropsWithRef, ReactNode } from 'react';
|
||||||
|
|
||||||
|
export type SupportLinkProps = Omit<
|
||||||
|
ComponentPropsWithRef<'button'>,
|
||||||
|
'children' | 'type'
|
||||||
|
> & {
|
||||||
|
/** «تماس با پشتیبانی»: the one label, never rephrased per screen. */
|
||||||
|
children: ReactNode;
|
||||||
|
/** false hides the headset icon. */
|
||||||
|
icon?: boolean;
|
||||||
|
/** A 44px hit area for a link on its own line. */
|
||||||
|
standalone?: boolean;
|
||||||
|
};
|
||||||
|
|
@ -0,0 +1,36 @@
|
||||||
|
import { cn } from '../../tools/cn';
|
||||||
|
import { FlexBox } from '../flex-box';
|
||||||
|
import { Icon } from '../icon';
|
||||||
|
import { Typography } from '../typography';
|
||||||
|
import { TAG_ICONS } from './constants';
|
||||||
|
import type { TagProps } from './types';
|
||||||
|
import { tagVariants } from './variants';
|
||||||
|
|
||||||
|
export function Tag({
|
||||||
|
variant = 'soft',
|
||||||
|
icon = TAG_ICONS[variant],
|
||||||
|
className,
|
||||||
|
children,
|
||||||
|
...props
|
||||||
|
}: TagProps) {
|
||||||
|
return (
|
||||||
|
<FlexBox
|
||||||
|
tag="span"
|
||||||
|
display="inline-flex"
|
||||||
|
alignItems="items-center"
|
||||||
|
gap="gap-1"
|
||||||
|
className={cn(tagVariants({ variant }), className)}
|
||||||
|
{...props}
|
||||||
|
>
|
||||||
|
{icon && <Icon name={icon} size={14} strokeWidth={2} />}
|
||||||
|
<Typography
|
||||||
|
tag="span"
|
||||||
|
variant="caption"
|
||||||
|
color="inherit"
|
||||||
|
className="font-semibold"
|
||||||
|
>
|
||||||
|
{children}
|
||||||
|
</Typography>
|
||||||
|
</FlexBox>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,6 @@
|
||||||
|
import type { IconName } from '../icon';
|
||||||
|
import type { TagVariant } from './types';
|
||||||
|
|
||||||
|
export const TAG_ICONS: Partial<Record<TagVariant, IconName>> = {
|
||||||
|
premium: 'star',
|
||||||
|
};
|
||||||
|
|
@ -0,0 +1,3 @@
|
||||||
|
export { Tag } from './Tag';
|
||||||
|
export type { TagProps, TagVariant } from './types';
|
||||||
|
export { tagVariants } from './variants';
|
||||||
|
|
@ -0,0 +1,16 @@
|
||||||
|
import type { VariantProps } from 'class-variance-authority';
|
||||||
|
import type { ComponentPropsWithRef, ReactNode } from 'react';
|
||||||
|
|
||||||
|
import type { IconName } from '../icon';
|
||||||
|
import type { tagVariants } from './variants';
|
||||||
|
|
||||||
|
export type TagVariant = NonNullable<
|
||||||
|
VariantProps<typeof tagVariants>['variant']
|
||||||
|
>;
|
||||||
|
|
||||||
|
export type TagProps = Omit<ComponentPropsWithRef<'span'>, 'children'> & {
|
||||||
|
variant?: TagVariant;
|
||||||
|
/** `null` hides the variant's default icon (premium shows a star). */
|
||||||
|
icon?: IconName | null;
|
||||||
|
children: ReactNode;
|
||||||
|
};
|
||||||
|
|
@ -0,0 +1,18 @@
|
||||||
|
import { cva } from 'class-variance-authority';
|
||||||
|
|
||||||
|
/** A label you read, not press: `radius-sm`, so it never looks tappable. */
|
||||||
|
export const tagVariants = cva('h-6 rounded-sm px-2 whitespace-nowrap', {
|
||||||
|
variants: {
|
||||||
|
variant: {
|
||||||
|
/** Markets. */
|
||||||
|
soft: 'bg-action-primary-subtle text-action-primary-text',
|
||||||
|
/** پریمیوم: night indigo with a saffron star. */
|
||||||
|
premium: 'bg-premium ps-1.5 pe-2 text-premium-on',
|
||||||
|
/** تخفیف: the one affirmative use of red. */
|
||||||
|
discount: 'bg-danger-solid text-text-on-action',
|
||||||
|
/** Counts, such as «۳ تصویر». */
|
||||||
|
neutral: 'border border-border-subtle bg-surface text-text-secondary',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
defaultVariants: { variant: 'soft' },
|
||||||
|
});
|
||||||
|
|
@ -0,0 +1,33 @@
|
||||||
|
import type { ElementType } from 'react';
|
||||||
|
|
||||||
|
import { Element } from '../../primitives/element';
|
||||||
|
import type { PolymorphicProps } from '../../primitives/element';
|
||||||
|
import { cn } from '../../tools/cn';
|
||||||
|
import type { TypographyOwnProps } from './types';
|
||||||
|
import { typographyVariants } from './variants';
|
||||||
|
|
||||||
|
export type TypographyProps<T extends ElementType = 'p'> = PolymorphicProps<
|
||||||
|
T,
|
||||||
|
TypographyOwnProps
|
||||||
|
>;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Every visible text. `tag` (default `p`) keeps the look and the semantic element independent, so
|
||||||
|
* a `heading-sm` can be a `span` and an `h2` can look like `body-lg`. Every number is already
|
||||||
|
* tabular through the root style.
|
||||||
|
*/
|
||||||
|
export function Typography<T extends ElementType = 'p'>({
|
||||||
|
tag,
|
||||||
|
variant,
|
||||||
|
color,
|
||||||
|
className,
|
||||||
|
...props
|
||||||
|
}: TypographyProps<T>) {
|
||||||
|
return (
|
||||||
|
<Element
|
||||||
|
className={cn(typographyVariants({ variant, color }), className)}
|
||||||
|
// The generic tag's props can't be checked inside the body; callers are checked above.
|
||||||
|
{...({ tag: tag ?? 'p', ...props } as Record<string, unknown>)}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,8 @@
|
||||||
|
export { Typography } from './Typography';
|
||||||
|
export type { TypographyProps } from './Typography';
|
||||||
|
export type {
|
||||||
|
TypographyColor,
|
||||||
|
TypographyOwnProps,
|
||||||
|
TypographyVariant,
|
||||||
|
} from './types';
|
||||||
|
export { typographyVariants } from './variants';
|
||||||
|
|
@ -0,0 +1,15 @@
|
||||||
|
import type { VariantProps } from 'class-variance-authority';
|
||||||
|
|
||||||
|
import type { typographyVariants } from './variants';
|
||||||
|
|
||||||
|
export type TypographyVariant = NonNullable<
|
||||||
|
VariantProps<typeof typographyVariants>['variant']
|
||||||
|
>;
|
||||||
|
export type TypographyColor = NonNullable<
|
||||||
|
VariantProps<typeof typographyVariants>['color']
|
||||||
|
>;
|
||||||
|
|
||||||
|
export type TypographyOwnProps = {
|
||||||
|
variant?: TypographyVariant;
|
||||||
|
color?: TypographyColor;
|
||||||
|
};
|
||||||
|
|
@ -0,0 +1,38 @@
|
||||||
|
import { cva } from 'class-variance-authority';
|
||||||
|
|
||||||
|
export const typographyVariants = cva('', {
|
||||||
|
variants: {
|
||||||
|
/** The design system's ten text styles; each sets size, line height and weight. */
|
||||||
|
variant: {
|
||||||
|
/** Takes size and weight from the parent, such as a label inside a button. */
|
||||||
|
inherit: '',
|
||||||
|
display: 'text-display',
|
||||||
|
'heading-lg': 'text-heading-lg',
|
||||||
|
'heading-md': 'text-heading-md',
|
||||||
|
'heading-sm': 'text-heading-sm',
|
||||||
|
'body-lg': 'text-body-lg',
|
||||||
|
'body-md': 'text-body-md',
|
||||||
|
'body-sm': 'text-body-sm',
|
||||||
|
label: 'text-label',
|
||||||
|
caption: 'text-caption',
|
||||||
|
micro: 'text-micro',
|
||||||
|
},
|
||||||
|
/** Semantic text colours. Status hues use their `-text` shade, which passes 4.5:1. */
|
||||||
|
color: {
|
||||||
|
inherit: '',
|
||||||
|
primary: 'text-text-primary',
|
||||||
|
secondary: 'text-text-secondary',
|
||||||
|
/** Placeholders and de-emphasised meta, on `surface` only. */
|
||||||
|
tertiary: 'text-text-tertiary',
|
||||||
|
inverse: 'text-text-inverse',
|
||||||
|
'on-action': 'text-text-on-action',
|
||||||
|
link: 'text-text-link',
|
||||||
|
brand: 'text-action-primary-text',
|
||||||
|
success: 'text-success-text',
|
||||||
|
danger: 'text-danger-text',
|
||||||
|
warning: 'text-warning-text',
|
||||||
|
premium: 'text-premium-text',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
defaultVariants: { variant: 'body-md', color: 'primary' },
|
||||||
|
});
|
||||||
|
|
@ -0,0 +1,2 @@
|
||||||
|
export { useCountdown } from './use-countdown';
|
||||||
|
export type { CountdownOptions } from './types';
|
||||||
|
|
@ -0,0 +1,10 @@
|
||||||
|
import type { DateInput } from '@lumina/tools/format';
|
||||||
|
|
||||||
|
export type CountdownOptions = {
|
||||||
|
/** Freezes the clock at this moment, for previews and tests. Also renders on the server. */
|
||||||
|
now?: DateInput;
|
||||||
|
/** How often the value changes: 1s for a clock, 60s for hours and days. */
|
||||||
|
intervalMs?: number;
|
||||||
|
/** Holds the value shown when it was paused; on resume it reads the wall clock again. */
|
||||||
|
paused?: boolean;
|
||||||
|
};
|
||||||
|
|
@ -0,0 +1,71 @@
|
||||||
|
'use client';
|
||||||
|
|
||||||
|
import type { DateInput } from '@lumina/tools/format';
|
||||||
|
import { useMemo, useSyncExternalStore } from 'react';
|
||||||
|
|
||||||
|
import type { CountdownOptions } from './types';
|
||||||
|
|
||||||
|
const NO_TICKS = () => () => {};
|
||||||
|
const UNKNOWN = () => null;
|
||||||
|
|
||||||
|
/** Time left rounded up to the whole interval, in seconds: stable between ticks. */
|
||||||
|
function secondsLeft(end: number, at: number, intervalMs: number): number {
|
||||||
|
return Math.max(0, (Math.ceil((end - at) / intervalMs) * intervalMs) / 1000);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Seconds left until `endsAt`, read from the wall clock, so a remount or a reload never restarts
|
||||||
|
* the wait. Ticks line up with the end (the clock reaches zero on time) and stop there.
|
||||||
|
*
|
||||||
|
* Returns `null` while the time is unknown: without `endsAt`, and during server render and
|
||||||
|
* hydration unless `now` is given. The server's clock and the browser's never agree to the second,
|
||||||
|
* so guessing would break hydration; a client-side navigation gets the value on its first render.
|
||||||
|
*/
|
||||||
|
export function useCountdown(
|
||||||
|
endsAt: DateInput | undefined,
|
||||||
|
{ now, intervalMs = 1000, paused = false }: CountdownOptions = {},
|
||||||
|
): number | null {
|
||||||
|
const end = endsAt == null ? null : new Date(endsAt).getTime();
|
||||||
|
const frozen = now == null ? null : new Date(now).getTime();
|
||||||
|
|
||||||
|
const subscribe = useMemo(() => {
|
||||||
|
if (end == null || frozen != null || paused) return NO_TICKS;
|
||||||
|
return (onTick: () => void) => {
|
||||||
|
let timer: ReturnType<typeof setTimeout> | undefined;
|
||||||
|
const schedule = () => {
|
||||||
|
const left = end - Date.now();
|
||||||
|
if (left <= 0) return;
|
||||||
|
// Wakes when the rounded value drops; an early wake-up just schedules the rest.
|
||||||
|
const wait = left - (Math.ceil(left / intervalMs) - 1) * intervalMs;
|
||||||
|
timer = setTimeout(() => {
|
||||||
|
onTick();
|
||||||
|
schedule();
|
||||||
|
}, wait);
|
||||||
|
};
|
||||||
|
schedule();
|
||||||
|
return () => clearTimeout(timer);
|
||||||
|
};
|
||||||
|
}, [end, frozen, paused, intervalMs]);
|
||||||
|
|
||||||
|
const getSnapshot = useMemo(() => {
|
||||||
|
if (end == null) return UNKNOWN;
|
||||||
|
if (frozen != null) {
|
||||||
|
const left = secondsLeft(end, frozen, intervalMs);
|
||||||
|
return () => left;
|
||||||
|
}
|
||||||
|
let held: number | undefined;
|
||||||
|
return () => {
|
||||||
|
const left = secondsLeft(end, Date.now(), intervalMs);
|
||||||
|
if (!paused) return left;
|
||||||
|
// The first read after pausing is the value on screen, so pausing never jumps.
|
||||||
|
held ??= left;
|
||||||
|
return held;
|
||||||
|
};
|
||||||
|
}, [end, frozen, paused, intervalMs]);
|
||||||
|
|
||||||
|
return useSyncExternalStore(
|
||||||
|
subscribe,
|
||||||
|
getSnapshot,
|
||||||
|
frozen == null ? UNKNOWN : getSnapshot,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1 @@
|
||||||
|
export { useOnline } from './use-online';
|
||||||
|
|
@ -0,0 +1,25 @@
|
||||||
|
'use client';
|
||||||
|
|
||||||
|
import { useSyncExternalStore } from 'react';
|
||||||
|
|
||||||
|
function subscribe(onChange: () => void) {
|
||||||
|
window.addEventListener('online', onChange);
|
||||||
|
window.addEventListener('offline', onChange);
|
||||||
|
return () => {
|
||||||
|
window.removeEventListener('online', onChange);
|
||||||
|
window.removeEventListener('offline', onChange);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
const getSnapshot = () => navigator.onLine;
|
||||||
|
|
||||||
|
/** The server can't know, and assuming online renders the normal page; the client corrects it. */
|
||||||
|
const getServerSnapshot = () => true;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Whether the device has a network connection, from the browser's `online` / `offline` events.
|
||||||
|
* `true` doesn't promise the server is reachable; a failed request still says so.
|
||||||
|
*/
|
||||||
|
export function useOnline(): boolean {
|
||||||
|
return useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot);
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,4 @@
|
||||||
|
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round">
|
||||||
|
<path d="M12 3.5L2.5 20h19z" />
|
||||||
|
<path d="M12 10v4.5M12 17.25v.01" />
|
||||||
|
</svg>
|
||||||
|
After Width: | Height: | Size: 242 B |
|
|
@ -0,0 +1,4 @@
|
||||||
|
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round">
|
||||||
|
<rect x="3" y="3.5" width="18" height="13" rx="2" />
|
||||||
|
<path d="M6.5 13l3.5-3.5 2.5 2.5 4.5-4.5M9 21l3-4.5 3 4.5" />
|
||||||
|
</svg>
|
||||||
|
After Width: | Height: | Size: 288 B |
|
|
@ -0,0 +1,3 @@
|
||||||
|
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round">
|
||||||
|
<path d="M12 5v14M6 13l6 6 6-6" />
|
||||||
|
</svg>
|
||||||
|
After Width: | Height: | Size: 206 B |
|
|
@ -0,0 +1,3 @@
|
||||||
|
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round">
|
||||||
|
<path d="M19 12H5M11 6l-6 6 6 6" />
|
||||||
|
</svg>
|
||||||
|
After Width: | Height: | Size: 207 B |
|
|
@ -0,0 +1,3 @@
|
||||||
|
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round">
|
||||||
|
<path d="M5 12h14M13 6l6 6-6 6" />
|
||||||
|
</svg>
|
||||||
|
After Width: | Height: | Size: 206 B |
|
|
@ -0,0 +1,3 @@
|
||||||
|
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round">
|
||||||
|
<path d="M12 19V5M6 11l6-6 6 6" />
|
||||||
|
</svg>
|
||||||
|
After Width: | Height: | Size: 206 B |
|
|
@ -0,0 +1,4 @@
|
||||||
|
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round">
|
||||||
|
<path d="M12 3.5a8.5 8.5 0 1 1-4.6 15.6L3.5 20.5l1.3-3.9A8.5 8.5 0 0 1 12 3.5z" />
|
||||||
|
<path d="M8.6 12.2l2.3 2.3 4.6-4.7" />
|
||||||
|
</svg>
|
||||||
|
After Width: | Height: | Size: 295 B |