diff --git a/packages/island/src/atoms/count-badge/CountBadge.tsx b/packages/island/src/atoms/count-badge/CountBadge.tsx new file mode 100644 index 0000000..96750fc --- /dev/null +++ b/packages/island/src/atoms/count-badge/CountBadge.tsx @@ -0,0 +1,33 @@ +import { number } from '@lumina/tools/format'; + +import { Element } from '../../primitives/element'; +import { cn } from '../../tools/cn'; +import type { CountBadgeProps } from './types'; + +/** + * An unread count on an icon, such as the bell's (U5 ۲.۲): white digits on danger, «۳», «۲۴», + * then «۹۹+». Nothing at 0. Decorative: the control's accessible name says the count, so the badge + * is hidden from screen readers. The caller places it, usually at the icon's top end. + */ +export function CountBadge({ + count, + cap = 99, + className, + ...props +}: CountBadgeProps) { + if (count <= 0) return null; + + return ( + + {count > cap ? `${number(cap)}+` : number(count)} + + ); +} diff --git a/packages/island/src/atoms/count-badge/index.ts b/packages/island/src/atoms/count-badge/index.ts new file mode 100644 index 0000000..29c883d --- /dev/null +++ b/packages/island/src/atoms/count-badge/index.ts @@ -0,0 +1,2 @@ +export { CountBadge } from './CountBadge'; +export type { CountBadgeProps } from './types'; diff --git a/packages/island/src/atoms/count-badge/types.ts b/packages/island/src/atoms/count-badge/types.ts new file mode 100644 index 0000000..8c001be --- /dev/null +++ b/packages/island/src/atoms/count-badge/types.ts @@ -0,0 +1,11 @@ +import type { ComponentPropsWithRef } from 'react'; + +export type CountBadgeProps = Omit< + ComponentPropsWithRef<'span'>, + 'children' +> & { + /** How many; nothing shows at 0. */ + count: number; + /** Above it the badge reads «۹۹+», so it never holds more than two digits and a plus. */ + cap?: number; +}; diff --git a/packages/island/src/atoms/date-time/DateTime.tsx b/packages/island/src/atoms/date-time/DateTime.tsx new file mode 100644 index 0000000..1114087 --- /dev/null +++ b/packages/island/src/atoms/date-time/DateTime.tsx @@ -0,0 +1,15 @@ +import { date, time } from '@lumina/tools/format'; + +import { Separator } from '../separator'; +import type { DateTimeProps } from './types'; + +/** A Shamsi day and its minute, apart: «۲ تیر ۱۴۰۴ | ۱۴:۲۳». */ +export function DateTime({ value }: DateTimeProps) { + return ( + <> + {date(value, { time: false })} + + {time(value)} + + ); +} diff --git a/packages/island/src/atoms/date-time/index.ts b/packages/island/src/atoms/date-time/index.ts new file mode 100644 index 0000000..3f3ed45 --- /dev/null +++ b/packages/island/src/atoms/date-time/index.ts @@ -0,0 +1,2 @@ +export { DateTime } from './DateTime'; +export type { DateTimeProps } from './types'; diff --git a/packages/island/src/atoms/date-time/types.ts b/packages/island/src/atoms/date-time/types.ts new file mode 100644 index 0000000..f6ad492 --- /dev/null +++ b/packages/island/src/atoms/date-time/types.ts @@ -0,0 +1,4 @@ +export type DateTimeProps = { + /** An ISO date-time. */ + value: string; +}; diff --git a/packages/island/src/atoms/spinner/Spinner.tsx b/packages/island/src/atoms/spinner/Spinner.tsx index 6565311..614c7a6 100644 --- a/packages/island/src/atoms/spinner/Spinner.tsx +++ b/packages/island/src/atoms/spinner/Spinner.tsx @@ -1,17 +1,18 @@ import { Element } from '../../primitives/element'; import { cn } from '../../tools/cn'; import type { SpinnerProps } from './types'; +import { spinnerVariants } from './variants'; -/** A ring that turns while one action is in flight. Never for loading a page: use Skeleton. */ -export function Spinner({ className, ...props }: SpinnerProps) { +/** + * A ring that turns while one action is in flight, or on the one screen that only says "wait". + * Never for loading a page: use Skeleton. + */ +export function Spinner({ size, className, ...props }: SpinnerProps) { return ( ); diff --git a/packages/island/src/atoms/spinner/index.ts b/packages/island/src/atoms/spinner/index.ts index f9ae700..f304aec 100644 --- a/packages/island/src/atoms/spinner/index.ts +++ b/packages/island/src/atoms/spinner/index.ts @@ -1,2 +1,3 @@ export { Spinner } from './Spinner'; export type { SpinnerProps } from './types'; +export { spinnerVariants } from './variants'; diff --git a/packages/island/src/atoms/spinner/types.ts b/packages/island/src/atoms/spinner/types.ts index ca18ded..fe2a321 100644 --- a/packages/island/src/atoms/spinner/types.ts +++ b/packages/island/src/atoms/spinner/types.ts @@ -1,3 +1,7 @@ +import type { VariantProps } from 'class-variance-authority'; import type { ComponentPropsWithRef } from 'react'; -export type SpinnerProps = Omit, 'children'>; +import type { spinnerVariants } from './variants'; + +export type SpinnerProps = Omit, 'children'> & + VariantProps; diff --git a/packages/island/src/atoms/spinner/variants.ts b/packages/island/src/atoms/spinner/variants.ts new file mode 100644 index 0000000..9da2253 --- /dev/null +++ b/packages/island/src/atoms/spinner/variants.ts @@ -0,0 +1,19 @@ +import { cva } from 'class-variance-authority'; + +export const spinnerVariants = cva( + 'inline-block flex-none animate-spin rounded-full', + { + variants: { + size: { + /** Inside a button or beside a label, in the text's colour. */ + inline: 'size-[1.125rem] border-2 border-current border-e-transparent', + /** + * A screen that exists only to say "wait", such as «در حال تأیید پرداخت...»: 40px, a muted + * ring with the brand arc. + */ + page: 'size-10 border-3 border-action-primary-muted border-e-action-primary', + }, + }, + defaultVariants: { size: 'inline' }, + }, +); diff --git a/packages/island/src/index.ts b/packages/island/src/index.ts index b71177a..80ad9ad 100644 --- a/packages/island/src/index.ts +++ b/packages/island/src/index.ts @@ -13,6 +13,8 @@ export { Checkbox, checkboxVariants } from './atoms/checkbox'; export type { CheckboxProps } from './atoms/checkbox'; export { Chip, chipVariants } from './atoms/chip'; export type { ChipProps, ChipSize } from './atoms/chip'; +export { CountBadge } from './atoms/count-badge'; +export type { CountBadgeProps } from './atoms/count-badge'; export { Countdown, countdownTone } from './atoms/countdown'; export type { CountdownKind, @@ -67,9 +69,11 @@ export { skeletonVariants, } from './atoms/skeleton'; export type { SkeletonProps, SkeletonRegionProps } from './atoms/skeleton'; +export { DateTime } from './atoms/date-time'; +export type { DateTimeProps } from './atoms/date-time'; export { Separator } from './atoms/separator'; export type { SeparatorProps } from './atoms/separator'; -export { Spinner } from './atoms/spinner'; +export { Spinner, spinnerVariants } from './atoms/spinner'; export { useCountdown } from './hooks/use-countdown'; export type { CountdownOptions } from './hooks/use-countdown'; export { useOnline } from './hooks/use-online'; @@ -113,12 +117,19 @@ export { ChipGroup } from './molecules/chip-group'; export type { ChipGroupOption, ChipGroupProps } from './molecules/chip-group'; export { ChoiceCard, choiceCardVariants } from './molecules/choice-card'; export type { ChoiceCardProps } from './molecules/choice-card'; +export { DetailRow, detailRowVariants } from './molecules/detail-row'; +export type { DetailRowProps } from './molecules/detail-row'; export { EmptyState } from './molecules/empty-state'; export type { EmptyStateProps, EmptyStateTone } from './molecules/empty-state'; export { ERROR_KIND_STYLES, ErrorState } from './molecules/error-state'; export type { ErrorKind, ErrorStateProps } from './molecules/error-state'; export { FactCard } from './molecules/fact-card'; export type { FactCardProps } from './molecules/fact-card'; +export { FilterChips } from './molecules/filter-chips'; +export type { + FilterChipOption, + FilterChipsProps, +} from './molecules/filter-chips'; export { FormSection } from './molecules/form-section'; export type { FormSectionProps } from './molecules/form-section'; export { LockedTeaser } from './molecules/locked-teaser'; diff --git a/packages/island/src/molecules/detail-row/DetailRow.tsx b/packages/island/src/molecules/detail-row/DetailRow.tsx new file mode 100644 index 0000000..5cae7b7 --- /dev/null +++ b/packages/island/src/molecules/detail-row/DetailRow.tsx @@ -0,0 +1,29 @@ +import { FlexBox } from '../../atoms/flex-box'; +import { Typography } from '../../atoms/typography'; +import { Element } from '../../primitives/element'; +import type { DetailRowProps } from './types'; +import { detailRowVariants } from './variants'; + +/** + * One line of a `
`, such as a receipt or a payment's detail: the label at the start, its + * value at the end, on one baseline. + */ +export function DetailRow({ label, children, size }: DetailRowProps) { + return ( + + + + {label} + + + + {children} + + + ); +} diff --git a/packages/island/src/molecules/detail-row/index.ts b/packages/island/src/molecules/detail-row/index.ts new file mode 100644 index 0000000..65b21be --- /dev/null +++ b/packages/island/src/molecules/detail-row/index.ts @@ -0,0 +1,3 @@ +export { DetailRow } from './DetailRow'; +export type { DetailRowProps } from './types'; +export { detailRowVariants } from './variants'; diff --git a/packages/island/src/molecules/detail-row/types.ts b/packages/island/src/molecules/detail-row/types.ts new file mode 100644 index 0000000..990763b --- /dev/null +++ b/packages/island/src/molecules/detail-row/types.ts @@ -0,0 +1,10 @@ +import type { VariantProps } from 'class-variance-authority'; +import type { ReactNode } from 'react'; + +import type { detailRowVariants } from './variants'; + +export type DetailRowProps = VariantProps & { + label: ReactNode; + /** The value, at the end. */ + children: ReactNode; +}; diff --git a/packages/island/src/molecules/detail-row/variants.ts b/packages/island/src/molecules/detail-row/variants.ts new file mode 100644 index 0000000..f116345 --- /dev/null +++ b/packages/island/src/molecules/detail-row/variants.ts @@ -0,0 +1,12 @@ +import { cva } from 'class-variance-authority'; + +/** `sm` for a receipt card, `md` for a detail sheet's divided list. */ +export const detailRowVariants = cva('', { + variants: { + size: { + sm: 'py-1.5', + md: 'py-2', + }, + }, + defaultVariants: { size: 'md' }, +}); diff --git a/packages/island/src/molecules/filter-chips/FilterChips.tsx b/packages/island/src/molecules/filter-chips/FilterChips.tsx new file mode 100644 index 0000000..2b1701a --- /dev/null +++ b/packages/island/src/molecules/filter-chips/FilterChips.tsx @@ -0,0 +1,64 @@ +import { useId } from 'react'; + +import { chipVariants } from '../../atoms/chip'; +import { FlexBox } from '../../atoms/flex-box'; +import { Typography } from '../../atoms/typography'; +import { Element } from '../../primitives/element'; +import { cn } from '../../tools/cn'; +import type { FilterChipsProps } from './types'; + +/** + * A one-of filter as a wrapping row of chips, such as a list's status (U4 ۳.۱): it applies at + * once, with no apply button. A named radiogroup of real radios, so Tab reaches the chosen chip + * and the arrow keys move the choice. The chosen chip fills brand; unlike a multiple-choice Chip + * it has no check, since exactly one is always chosen. + */ +export function FilterChips({ + options, + value, + onChange, + label, + size = 'md', + disabled = false, + className, +}: FilterChipsProps) { + const name = useId(); + + return ( + + {options.map((option) => ( + + onChange(option.value)} + className="pointer-events-none absolute m-0 size-px opacity-0" + /> + + {option.label} + + + ))} + + ); +} diff --git a/packages/island/src/molecules/filter-chips/index.ts b/packages/island/src/molecules/filter-chips/index.ts new file mode 100644 index 0000000..f4484b2 --- /dev/null +++ b/packages/island/src/molecules/filter-chips/index.ts @@ -0,0 +1,2 @@ +export { FilterChips } from './FilterChips'; +export type { FilterChipOption, FilterChipsProps } from './types'; diff --git a/packages/island/src/molecules/filter-chips/types.ts b/packages/island/src/molecules/filter-chips/types.ts new file mode 100644 index 0000000..01bab22 --- /dev/null +++ b/packages/island/src/molecules/filter-chips/types.ts @@ -0,0 +1,20 @@ +import type { ReactNode } from 'react'; + +import type { ChipSize } from '../../atoms/chip'; + +export type FilterChipOption = { value: T; label: ReactNode }; + +export type FilterChipsProps = { + /** In reading order, such as «همه» · «موفق» · «در حال بررسی» · «ناموفق». */ + options: readonly FilterChipOption[]; + /** The one shown now. */ + value: T; + /** Called with the chip picked by a press or the arrow keys; never for the chosen one. */ + onChange: (value: T) => void; + /** The group's accessible name, such as «وضعیت». */ + label: string; + /** `md` (44px) on a page, `sm` (36px) in a compact row. */ + size?: ChipSize; + disabled?: boolean; + className?: string; +}; diff --git a/packages/island/src/molecules/media-carousel/MediaCarousel.tsx b/packages/island/src/molecules/media-carousel/MediaCarousel.tsx index c661068..eb6a2e6 100644 --- a/packages/island/src/molecules/media-carousel/MediaCarousel.tsx +++ b/packages/island/src/molecules/media-carousel/MediaCarousel.tsx @@ -5,7 +5,8 @@ import type { MediaCarouselProps } from './types'; /** * A post's images. One shows full width at its own ratio, letterboxed at 400px on - * `surface-sunken`; 2–4 swipe in 280px slides with dots and a «۱ / ۳» count. Server-renderable: + * `surface-sunken`; 2–4 swipe in 280px slides with dots and a «۱ / ۳» count, and from 480px wide + * also move with ‹ › buttons on the image. Server-renderable: * a single image ships no JavaScript, and only the swipe track is a client component, so every * prop it takes is serializable except `onOpen`, which only a client caller can pass. */ @@ -15,6 +16,8 @@ export function MediaCarousel({ href, onOpen, label, + previousLabel, + nextLabel, className, }: MediaCarouselProps) { const shown = items.slice(0, MAX_MEDIA); @@ -42,6 +45,8 @@ export function MediaCarousel({ href={href} onOpen={onOpen} label={label} + previousLabel={previousLabel} + nextLabel={nextLabel} className={className} /> ); diff --git a/packages/island/src/molecules/media-carousel/components/MediaNavButton.tsx b/packages/island/src/molecules/media-carousel/components/MediaNavButton.tsx new file mode 100644 index 0000000..c3eaf2e --- /dev/null +++ b/packages/island/src/molecules/media-carousel/components/MediaNavButton.tsx @@ -0,0 +1,30 @@ +import { GridBox } from '../../../atoms/grid-box'; +import { Icon } from '../../../atoms/icon'; +import type { MediaNavButtonProps } from '../types'; +import { mediaNavVariants } from '../variants'; + +/** A ‹ › button on the track; its chevron points the way it moves, flipped in LTR. */ +export function MediaNavButton({ + side, + label, + isDisabled, + onClick, +}: MediaNavButtonProps) { + return ( + + + + ); +} diff --git a/packages/island/src/molecules/media-carousel/components/MediaSlide.tsx b/packages/island/src/molecules/media-carousel/components/MediaSlide.tsx index 13a0632..3ba0a42 100644 --- a/packages/island/src/molecules/media-carousel/components/MediaSlide.tsx +++ b/packages/island/src/molecules/media-carousel/components/MediaSlide.tsx @@ -46,7 +46,7 @@ export function MediaSlide({ className={cn( box, 'actionable:cursor-zoom-in', - // A slide's ring would be clipped by the scrolling track, so the track's frame draws it. + // A slide's ring would be clipped by the scrolling track, so the track draws it. single ? 'focus-visible:focus-ring' : 'outline-none', )} > diff --git a/packages/island/src/molecules/media-carousel/components/MediaTrack.tsx b/packages/island/src/molecules/media-carousel/components/MediaTrack.tsx index d602fab..fc9bf72 100644 --- a/packages/island/src/molecules/media-carousel/components/MediaTrack.tsx +++ b/packages/island/src/molecules/media-carousel/components/MediaTrack.tsx @@ -1,19 +1,25 @@ 'use client'; import { number } from '@lumina/tools/format'; -import { useState } from 'react'; -import type { UIEvent } from 'react'; +import { useRef, useState } from 'react'; +import type { KeyboardEvent, UIEvent } from 'react'; import { FlexBox } from '../../../atoms/flex-box'; import { Typography } from '../../../atoms/typography'; import { Element } from '../../../primitives/element'; +import { cn } from '../../../tools/cn'; import type { MediaCarouselProps } from '../types'; import { mediaDotVariants } from '../variants'; +import { MediaNavButton } from './MediaNavButton'; import { MediaSlide } from './MediaSlide'; +const isRtl = (node: HTMLElement) => getComputedStyle(node).direction === 'rtl'; + /** - * 2–4 images in a native swipe (scroll snap), with a «۱ / ۳» chip and a dot per image. The only - * client part of a post: it follows the scroll to light the current dot and count. + * 2–4 images in a native swipe (scroll snap), with a «۱ / ۳» count and a dot per image. From 480px + * wide, ‹ › buttons on the image move it too, and ← → on a focused slide. Moving never opens the + * Lightbox: only a tap on an image does. The only client part of a post: it follows the scroll to + * light the current dot and count. */ export function MediaTrack({ items, @@ -21,9 +27,12 @@ export function MediaTrack({ href, onOpen, label, + previousLabel, + nextLabel, className, }: MediaCarouselProps) { const [current, setCurrent] = useState(0); + const trackRef = useRef(null); const count = items.length; // scrollLeft is negative in RTL, so its size gives the slide either way. @@ -33,18 +42,52 @@ export function MediaTrack({ setCurrent(Math.min(count - 1, index)); }; + // Only scrolls: `handleScroll` then moves the dots and the count. + const goTo = (index: number) => { + const track = trackRef.current; + if (!track) return; + const reduced = window.matchMedia( + '(prefers-reduced-motion: reduce)', + ).matches; + track.scrollTo({ + left: (isRtl(track) ? -1 : 1) * index * track.clientWidth, + behavior: reduced ? 'auto' : 'smooth', + }); + }; + + // ← is next in RTL, as in the Lightbox. Focus moves with the image, so Enter opens the one shown. + const handleKeyDown = (event: KeyboardEvent) => { + const track = event.currentTarget; + const slides = Array.from(track.children) as HTMLElement[]; + const from = slides.indexOf(event.target as HTMLElement); + if (from === -1) return; + const [next, previous] = isRtl(track) + ? ['ArrowLeft', 'ArrowRight'] + : ['ArrowRight', 'ArrowLeft']; + const step = event.key === next ? 1 : event.key === previous ? -1 : 0; + if (step === 0) return; + event.preventDefault(); + const to = Math.min(Math.max(from + step, 0), count - 1); + if (to === from) return; + slides[to].focus({ preventScroll: true }); + goTo(to); + }; + return ( - + {items.map((item, index) => ( ))} - {/* Each slide button already says «۱ از ۳»; the chip is for the eye only. */} + {previousLabel && nextLabel && ( + <> + goTo(current - 1)} + /> + goTo(current + 1)} + /> + + )} diff --git a/packages/island/src/molecules/media-carousel/types.ts b/packages/island/src/molecules/media-carousel/types.ts index cfab787..90d44aa 100644 --- a/packages/island/src/molecules/media-carousel/types.ts +++ b/packages/island/src/molecules/media-carousel/types.ts @@ -26,6 +26,12 @@ export type MediaCarouselProps = { onOpen?: (index: number) => void; /** The swipe section's accessible name, such as «تصاویر پست». */ label?: string; + /** + * The ‹ › buttons' names, «تصویر قبلی» and «تصویر بعدی». The buttons show only with both, on a + * carousel at least 480px wide. + */ + previousLabel?: string; + nextLabel?: string; className?: string; }; @@ -40,3 +46,12 @@ export type MediaSlideProps = { onOpen?: (index: number) => void; className?: string; }; + +export type MediaNavButtonProps = { + /** Previous sits at inline-start, next at inline-end. */ + side: 'previous' | 'next'; + label: string; + /** At the first or last image: frozen, but it keeps focus. */ + isDisabled: boolean; + onClick: () => void; +}; diff --git a/packages/island/src/molecules/media-carousel/variants.ts b/packages/island/src/molecules/media-carousel/variants.ts index 7991fb9..13fba11 100644 --- a/packages/island/src/molecules/media-carousel/variants.ts +++ b/packages/island/src/molecules/media-carousel/variants.ts @@ -33,3 +33,20 @@ export const mediaDotVariants = cva( defaultVariants: { active: false }, }, ); + +/** + * A 44px ‹ › control on the image, vertically centred. Hidden until the carousel (its `@container`) + * is 480px wide, so phones swipe. Ends use `aria-disabled`, so a focused button keeps focus when + * it runs out. + */ +export const mediaNavVariants = cva( + 'absolute top-1/2 z-1 hidden size-touch-target -translate-y-1/2 rounded-full bg-image-control text-text-primary shadow-2 transition-[opacity,background-color] duration-120 ease-standard @min-[480px]:grid focus-visible:focus-ring actionable:cursor-pointer actionable:hover:bg-surface aria-disabled:frozen', + { + variants: { + side: { + previous: 'start-3', + next: 'end-3', + }, + }, + }, +); diff --git a/packages/island/src/organisms/post-card/components/PostContent.tsx b/packages/island/src/organisms/post-card/components/PostContent.tsx index b2fda31..773005d 100644 --- a/packages/island/src/organisms/post-card/components/PostContent.tsx +++ b/packages/island/src/organisms/post-card/components/PostContent.tsx @@ -46,6 +46,8 @@ export function PostContent({ href={href} onOpen={onImageClick} label={labels.images} + previousLabel={labels.previousImage} + nextLabel={labels.nextImage} className={ABOVE_LINK_CLASS} /> )} diff --git a/packages/island/src/organisms/post-card/types.ts b/packages/island/src/organisms/post-card/types.ts index a1f15fc..8e3e181 100644 --- a/packages/island/src/organisms/post-card/types.ts +++ b/packages/island/src/organisms/post-card/types.ts @@ -65,6 +65,9 @@ export type PostCardLabels = { * تصویر ۱ از ۳، نمودار طلا». */ openSlide?: (n: string, total: string, alt: string) => string; + /** The carousel's ‹ › buttons, «تصویر قبلی» and «تصویر بعدی»; they show only with both. */ + previousImage?: string; + nextImage?: string; /** A locked post's image count, from 2 images: `(n) => <>{n} تصویر` → «۳ تصویر». */ imageCount: (count: string) => ReactNode; /** «اشتراک‌گذاری». */ diff --git a/packages/island/src/styles/theme.css b/packages/island/src/styles/theme.css index 92750d5..6cdc3df 100644 --- a/packages/island/src/styles/theme.css +++ b/packages/island/src/styles/theme.css @@ -250,6 +250,8 @@ --inverse-control-hover: rgba(255, 255, 255, 0.2); /* Counters drawn over images («۱ / ۳»). */ --scrim-chip: rgba(12, 16, 24, 0.64); + /* Controls drawn over images (the post carousel's ‹ ›): near-opaque white pills. */ + --image-control: rgba(255, 255, 255, 0.94); --text-on-action: #ffffff; --text-link: var(--indigo-600); --border-subtle: var(--slate-150); @@ -319,6 +321,7 @@ --color-inverse-control: var(--inverse-control); --color-inverse-control-hover: var(--inverse-control-hover); --color-scrim-chip: var(--scrim-chip); + --color-image-control: var(--image-control); --color-text-on-action: var(--text-on-action); --color-text-link: var(--text-link); --color-border-subtle: var(--border-subtle);