feat(island,tools): select, switch, info tip, jalali date helpers
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
381e01e030
commit
db0fa7c86e
|
|
@ -0,0 +1,59 @@
|
|||
import { useId } from 'react';
|
||||
|
||||
import { Element } from '../../primitives/element';
|
||||
import { cn } from '../../tools/cn';
|
||||
import { FlexBox } from '../flex-box';
|
||||
import { Icon } from '../icon';
|
||||
import { Typography } from '../typography';
|
||||
import type { SelectProps } from './types';
|
||||
|
||||
/**
|
||||
* A labelled native select, 44px high. On phones the system draws its own wheel or list, so a
|
||||
* date or time picker built from these needs no script of its own.
|
||||
*/
|
||||
export function Select({
|
||||
label,
|
||||
options,
|
||||
className,
|
||||
id,
|
||||
...props
|
||||
}: SelectProps) {
|
||||
const fallbackId = useId();
|
||||
const selectId = id ?? fallbackId;
|
||||
|
||||
return (
|
||||
<FlexBox
|
||||
direction="flex-col"
|
||||
gap="gap-1"
|
||||
className={cn('min-w-0', className)}
|
||||
>
|
||||
<Typography
|
||||
tag="label"
|
||||
htmlFor={selectId}
|
||||
variant="caption"
|
||||
color="secondary"
|
||||
>
|
||||
{label}
|
||||
</Typography>
|
||||
<FlexBox tag="span" alignItems="items-center" className="relative">
|
||||
<Element
|
||||
tag="select"
|
||||
id={selectId}
|
||||
className="h-touch-target w-full cursor-pointer appearance-none rounded-md border border-border-default bg-surface ps-3 pe-8 text-body-md text-text-primary outline-none transition-colors duration-120 ease-standard hover:border-border-strong focus-visible:focus-ring disabled:cursor-not-allowed disabled:opacity-(--opacity-dimmed)"
|
||||
{...props}
|
||||
>
|
||||
{options.map((option) => (
|
||||
<Element tag="option" key={option.value} value={option.value}>
|
||||
{option.label}
|
||||
</Element>
|
||||
))}
|
||||
</Element>
|
||||
<Icon
|
||||
name="chevron-down"
|
||||
size={16}
|
||||
className="pointer-events-none absolute end-3 text-text-secondary"
|
||||
/>
|
||||
</FlexBox>
|
||||
</FlexBox>
|
||||
);
|
||||
}
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
export { Select } from './Select';
|
||||
export type { SelectOption, SelectProps } from './types';
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
import type { ComponentPropsWithRef, ReactNode } from 'react';
|
||||
|
||||
export type SelectOption = { value: string; label: ReactNode };
|
||||
|
||||
export type SelectProps = Omit<ComponentPropsWithRef<'select'>, 'children'> & {
|
||||
/** The visible label above the control, such as «روز». */
|
||||
label: ReactNode;
|
||||
options: readonly SelectOption[];
|
||||
};
|
||||
|
|
@ -0,0 +1,58 @@
|
|||
'use client';
|
||||
|
||||
import { Element } from '../../primitives/element';
|
||||
import { cn } from '../../tools/cn';
|
||||
import { Typography } from '../typography';
|
||||
import type { SwitchProps } from './types';
|
||||
import { switchKnobVariants, switchTrackVariants } from './variants';
|
||||
|
||||
/**
|
||||
* An on/off control that applies to a form, such as offering a period or running a promotion. It
|
||||
* is a `role="switch"` button in a 44px hit area; with `children` the label is part of it. A locked
|
||||
* switch stays focusable and says why elsewhere; a disabled one is never used.
|
||||
*/
|
||||
export function Switch({
|
||||
checked,
|
||||
onCheckedChange,
|
||||
locked = false,
|
||||
children,
|
||||
className,
|
||||
onClick,
|
||||
...props
|
||||
}: SwitchProps) {
|
||||
return (
|
||||
<Element
|
||||
tag="button"
|
||||
type="button"
|
||||
role="switch"
|
||||
aria-checked={checked}
|
||||
aria-disabled={locked || undefined}
|
||||
onClick={(event) => {
|
||||
onClick?.(event);
|
||||
if (!locked && !event.defaultPrevented) onCheckedChange?.(!checked);
|
||||
}}
|
||||
className={cn(
|
||||
'inline-flex min-h-touch-target min-w-touch-target items-center justify-center gap-3 rounded-md outline-none focus-visible:focus-ring',
|
||||
locked
|
||||
? 'cursor-not-allowed opacity-(--opacity-dimmed)'
|
||||
: 'cursor-pointer',
|
||||
children && 'justify-between text-start',
|
||||
className,
|
||||
)}
|
||||
{...props}
|
||||
>
|
||||
{children && (
|
||||
<Typography tag="span" variant="body-sm" className="font-semibold">
|
||||
{children}
|
||||
</Typography>
|
||||
)}
|
||||
<Element
|
||||
tag="span"
|
||||
aria-hidden
|
||||
className={switchTrackVariants({ checked })}
|
||||
>
|
||||
<Element tag="span" className={switchKnobVariants({ checked })} />
|
||||
</Element>
|
||||
</Element>
|
||||
);
|
||||
}
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
export { Switch } from './Switch';
|
||||
export type { SwitchProps } from './types';
|
||||
export { switchKnobVariants, switchTrackVariants } from './variants';
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
import type { ComponentPropsWithRef, ReactNode } from 'react';
|
||||
|
||||
export type SwitchProps = Omit<
|
||||
ComponentPropsWithRef<'button'>,
|
||||
'type' | 'role' | 'onChange' | 'children'
|
||||
> & {
|
||||
checked: boolean;
|
||||
onCheckedChange?: (checked: boolean) => void;
|
||||
/**
|
||||
* Visible text beside the switch, such as «تخفیف دورهای»; the whole row is then the hit area.
|
||||
* Without it, give the switch an `aria-label`.
|
||||
*/
|
||||
children?: ReactNode;
|
||||
/** Can't change now, such as a period during the 7-day lock: 50%, `aria-disabled`, still focusable. */
|
||||
locked?: boolean;
|
||||
};
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
import { cva } from 'class-variance-authority';
|
||||
|
||||
/** The 44×24 track; the knob slides to the end when on, mirrored in RTL. */
|
||||
export const switchTrackVariants = cva(
|
||||
'relative inline-flex h-6 w-11 flex-none items-center rounded-full border-[1.5px] p-0.5 transition-colors duration-120 ease-standard',
|
||||
{
|
||||
variants: {
|
||||
checked: {
|
||||
true: 'border-action-primary bg-action-primary',
|
||||
false: 'border-border-default bg-surface-sunken',
|
||||
},
|
||||
},
|
||||
defaultVariants: { checked: false },
|
||||
},
|
||||
);
|
||||
|
||||
export const switchKnobVariants = cva(
|
||||
'size-4.5 rounded-full shadow-1 transition-transform duration-120 ease-standard motion-reduce:transition-none',
|
||||
{
|
||||
variants: {
|
||||
checked: {
|
||||
true: 'translate-x-[-1.25rem] bg-text-on-action ltr:translate-x-5',
|
||||
false: 'translate-x-0 bg-surface',
|
||||
},
|
||||
},
|
||||
defaultVariants: { checked: false },
|
||||
},
|
||||
);
|
||||
|
|
@ -92,6 +92,14 @@ export type {
|
|||
} from './atoms/status-badge';
|
||||
export { SupportLink } from './atoms/support-link';
|
||||
export type { SupportLinkProps } from './atoms/support-link';
|
||||
export { Select } from './atoms/select';
|
||||
export type { SelectOption, SelectProps } from './atoms/select';
|
||||
export {
|
||||
Switch,
|
||||
switchKnobVariants,
|
||||
switchTrackVariants,
|
||||
} from './atoms/switch';
|
||||
export type { SwitchProps } from './atoms/switch';
|
||||
export { Tag, tagVariants } from './atoms/tag';
|
||||
export type { TagProps, TagVariant } from './atoms/tag';
|
||||
export { Typography, typographyVariants } from './atoms/typography';
|
||||
|
|
@ -132,6 +140,8 @@ export type {
|
|||
} from './molecules/filter-chips';
|
||||
export { FormSection } from './molecules/form-section';
|
||||
export type { FormSectionProps } from './molecules/form-section';
|
||||
export { InfoTip, infoTipVariants } from './molecules/info-tip';
|
||||
export type { InfoTipProps } from './molecules/info-tip';
|
||||
export { LockedTeaser } from './molecules/locked-teaser';
|
||||
export type { LockedTeaserProps } from './molecules/locked-teaser';
|
||||
export { MediaCarousel } from './molecules/media-carousel';
|
||||
|
|
|
|||
|
|
@ -0,0 +1,71 @@
|
|||
'use client';
|
||||
|
||||
import { useEffect, useId, useRef, useState } from 'react';
|
||||
|
||||
import { Icon } from '../../atoms/icon';
|
||||
import { Typography } from '../../atoms/typography';
|
||||
import { Element } from '../../primitives/element';
|
||||
import { cn } from '../../tools/cn';
|
||||
import type { InfoTipProps } from './types';
|
||||
import { infoTipVariants } from './variants';
|
||||
|
||||
/**
|
||||
* An ⓘ that explains the figure beside it. Hover and keyboard focus show the text; a tap toggles
|
||||
* it, and a tap anywhere else or Escape closes it, because touch has no hover. The button is 44px;
|
||||
* the icon 16.
|
||||
*/
|
||||
export function InfoTip({ label, children, className }: InfoTipProps) {
|
||||
const [isOpen, setIsOpen] = useState(false);
|
||||
const tipId = useId();
|
||||
const rootRef = useRef<HTMLSpanElement>(null);
|
||||
|
||||
useEffect(() => {
|
||||
if (!isOpen) return;
|
||||
const close = (event: Event) => {
|
||||
if (event instanceof KeyboardEvent && event.key !== 'Escape') return;
|
||||
if (
|
||||
event instanceof PointerEvent &&
|
||||
rootRef.current?.contains(event.target as Node)
|
||||
)
|
||||
return;
|
||||
setIsOpen(false);
|
||||
};
|
||||
document.addEventListener('pointerdown', close);
|
||||
document.addEventListener('keydown', close);
|
||||
return () => {
|
||||
document.removeEventListener('pointerdown', close);
|
||||
document.removeEventListener('keydown', close);
|
||||
};
|
||||
}, [isOpen]);
|
||||
|
||||
return (
|
||||
<Element
|
||||
tag="span"
|
||||
ref={rootRef}
|
||||
className={cn('group relative -my-3 inline-flex', className)}
|
||||
onMouseLeave={() => setIsOpen(false)}
|
||||
>
|
||||
<Element
|
||||
tag="button"
|
||||
type="button"
|
||||
aria-label={label}
|
||||
aria-describedby={tipId}
|
||||
aria-expanded={isOpen}
|
||||
onClick={() => setIsOpen((open) => !open)}
|
||||
className="inline-flex size-touch-target cursor-pointer items-center justify-center rounded-full text-text-secondary outline-none hover:text-text-primary focus-visible:focus-ring"
|
||||
>
|
||||
<Icon name="info" size={16} />
|
||||
</Element>
|
||||
<Typography
|
||||
tag="span"
|
||||
id={tipId}
|
||||
role="tooltip"
|
||||
variant="caption"
|
||||
color="inverse"
|
||||
className={infoTipVariants({ open: isOpen })}
|
||||
>
|
||||
{children}
|
||||
</Typography>
|
||||
</Element>
|
||||
);
|
||||
}
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
export { InfoTip } from './InfoTip';
|
||||
export type { InfoTipProps } from './types';
|
||||
export { infoTipVariants } from './variants';
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
import type { ReactNode } from 'react';
|
||||
|
||||
export type InfoTipProps = {
|
||||
/** The button's accessible name, such as «توضیح قابل واریز». */
|
||||
label: string;
|
||||
/** What the figure means, one or two sentences. */
|
||||
children: ReactNode;
|
||||
className?: string;
|
||||
};
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
import { cva } from 'class-variance-authority';
|
||||
|
||||
/** Under the ⓘ, its start on the icon's, on the inverse surface. */
|
||||
export const infoTipVariants = cva(
|
||||
'pointer-events-none absolute start-0 top-[calc(100%+0.25rem)] z-menu hidden w-max max-w-[16.25rem] rounded-sm bg-surface-inverse px-3 py-1.5 text-start whitespace-normal shadow-2 group-hover:block group-has-[:focus-visible]:block',
|
||||
{
|
||||
variants: {
|
||||
open: { true: 'block', false: '' },
|
||||
},
|
||||
defaultVariants: { open: false },
|
||||
},
|
||||
);
|
||||
|
|
@ -21,3 +21,10 @@ export type {
|
|||
DateInput,
|
||||
ShebaFormatOptions,
|
||||
} from './format';
|
||||
export {
|
||||
fromJalali,
|
||||
isJalaliLeapYear,
|
||||
jalaliMonthLength,
|
||||
toJalali,
|
||||
} from './jalali';
|
||||
export type { JalaliDateTime } from './jalali';
|
||||
|
|
|
|||
|
|
@ -0,0 +1,12 @@
|
|||
/** Tehran's offset from UTC. Iran dropped daylight saving in 2022, so it is fixed. */
|
||||
export const TEHRAN_OFFSET_MINUTES = 210;
|
||||
|
||||
/** The years the calendar arithmetic covers (`BREAKS`' first and last). */
|
||||
export const MIN_JALALI_YEAR = -61;
|
||||
export const MAX_JALALI_YEAR = 3177;
|
||||
|
||||
/** The Jalali leap-cycle break years (Borkowski), the table the arithmetic below needs. */
|
||||
export const BREAKS = [
|
||||
-61, 9, 38, 199, 426, 686, 756, 818, 1111, 1181, 1210, 1635, 2060, 2097, 2192,
|
||||
2262, 2324, 2394, 2456, 3178,
|
||||
] as const;
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
export {
|
||||
fromJalali,
|
||||
isJalaliLeapYear,
|
||||
jalaliMonthLength,
|
||||
toJalali,
|
||||
} from './jalali';
|
||||
export type { JalaliDateTime } from './types';
|
||||
|
|
@ -0,0 +1,129 @@
|
|||
import type { DateInput } from '../format';
|
||||
import {
|
||||
BREAKS,
|
||||
MAX_JALALI_YEAR,
|
||||
MIN_JALALI_YEAR,
|
||||
TEHRAN_OFFSET_MINUTES,
|
||||
} from './constants';
|
||||
import type { JalaliDateTime } from './types';
|
||||
|
||||
// Jalali ↔ Gregorian through Julian day numbers (Borkowski's algorithm, as in jalaali-js).
|
||||
|
||||
const MINUTE_MS = 60_000;
|
||||
|
||||
function div(a: number, b: number): number {
|
||||
return Math.trunc(a / b);
|
||||
}
|
||||
|
||||
function mod(a: number, b: number): number {
|
||||
return a - Math.trunc(a / b) * b;
|
||||
}
|
||||
|
||||
/** The Gregorian year that starts in a Jalali year, the March day of its Nowruz, and its leap state. */
|
||||
function jalCal(jy: number): { leap: number; gy: number; march: number } {
|
||||
if (jy < MIN_JALALI_YEAR || jy > MAX_JALALI_YEAR) {
|
||||
throw new RangeError(`Jalali year ${jy} is out of range`);
|
||||
}
|
||||
const gy = jy + 621;
|
||||
let leapJ = -14;
|
||||
let jp: number = BREAKS[0];
|
||||
let jump = 0;
|
||||
for (let i = 1; i < BREAKS.length; i += 1) {
|
||||
const jm = BREAKS[i];
|
||||
jump = jm - jp;
|
||||
if (jy < jm) break;
|
||||
leapJ += div(jump, 33) * 8 + div(mod(jump, 33), 4);
|
||||
jp = jm;
|
||||
}
|
||||
let n = jy - jp;
|
||||
leapJ += div(n, 33) * 8 + div(mod(n, 33) + 3, 4);
|
||||
if (mod(jump, 33) === 4 && jump - n === 4) leapJ += 1;
|
||||
const leapG = div(gy, 4) - div((div(gy, 100) + 1) * 3, 4) - 150;
|
||||
const march = 20 + leapJ - leapG;
|
||||
if (jump - n < 6) n = n - jump + div(jump + 4, 33) * 33;
|
||||
let leap = mod(mod(n + 1, 33) - 1, 4);
|
||||
if (leap === -1) leap = 4;
|
||||
return { leap, gy, march };
|
||||
}
|
||||
|
||||
function g2d(gy: number, gm: number, gd: number): number {
|
||||
const d =
|
||||
div((gy + div(gm - 8, 6) + 100100) * 1461, 4) +
|
||||
div(153 * mod(gm + 9, 12) + 2, 5) +
|
||||
gd -
|
||||
34840408;
|
||||
return d - div(div(gy + 100100 + div(gm - 8, 6), 100) * 3, 4) + 752;
|
||||
}
|
||||
|
||||
function d2g(jdn: number): { gy: number; gm: number; gd: number } {
|
||||
let j = 4 * jdn + 139361631;
|
||||
j += div(div(4 * jdn + 183187720, 146097) * 3, 4) * 4 - 3908;
|
||||
const i = div(mod(j, 1461), 4) * 5 + 308;
|
||||
const gd = div(mod(i, 153), 5) + 1;
|
||||
const gm = mod(div(i, 153), 12) + 1;
|
||||
const gy = div(j, 1461) - 100100 + div(8 - gm, 6);
|
||||
return { gy, gm, gd };
|
||||
}
|
||||
|
||||
function j2d(jy: number, jm: number, jd: number): number {
|
||||
const { gy, march } = jalCal(jy);
|
||||
return g2d(gy, 3, march) + (jm - 1) * 31 - div(jm, 7) * (jm - 7) + jd - 1;
|
||||
}
|
||||
|
||||
function d2j(jdn: number): { jy: number; jm: number; jd: number } {
|
||||
const { gy } = d2g(jdn);
|
||||
let jy = gy - 621;
|
||||
const { leap, march } = jalCal(jy);
|
||||
let k = jdn - g2d(gy, 3, march);
|
||||
if (k >= 0) {
|
||||
if (k <= 185) return { jy, jm: 1 + div(k, 31), jd: mod(k, 31) + 1 };
|
||||
k -= 186;
|
||||
} else {
|
||||
jy -= 1;
|
||||
k += 179;
|
||||
if (leap === 1) k += 1;
|
||||
}
|
||||
return { jy, jm: 7 + div(k, 30), jd: mod(k, 30) + 1 };
|
||||
}
|
||||
|
||||
/** Whether a Shamsi year has 30 days in اسفند. */
|
||||
export function isJalaliLeapYear(year: number): boolean {
|
||||
return jalCal(year).leap === 0;
|
||||
}
|
||||
|
||||
/** Days in a Shamsi month: 31 for the first six, 30 for the next five, 29 or 30 for اسفند. */
|
||||
export function jalaliMonthLength(year: number, month: number): number {
|
||||
if (month <= 6) return 31;
|
||||
if (month <= 11) return 30;
|
||||
return isJalaliLeapYear(year) ? 30 : 29;
|
||||
}
|
||||
|
||||
/** A moment as Tehran's Shamsi wall clock: `2026-10-02T19:00:00Z` → 1405/7/10 22:30. */
|
||||
export function toJalali(value: DateInput): JalaliDateTime {
|
||||
const at = value instanceof Date ? value : new Date(value);
|
||||
const local = new Date(at.getTime() + TEHRAN_OFFSET_MINUTES * MINUTE_MS);
|
||||
const { jy, jm, jd } = d2j(
|
||||
g2d(local.getUTCFullYear(), local.getUTCMonth() + 1, local.getUTCDate()),
|
||||
);
|
||||
return {
|
||||
year: jy,
|
||||
month: jm,
|
||||
day: jd,
|
||||
hour: local.getUTCHours(),
|
||||
minute: local.getUTCMinutes(),
|
||||
};
|
||||
}
|
||||
|
||||
/** Tehran's Shamsi wall clock as an ISO instant: 1405/7/10 22:30 → `2026-10-02T19:00:00.000Z`. */
|
||||
export function fromJalali({
|
||||
year,
|
||||
month,
|
||||
day,
|
||||
hour,
|
||||
minute,
|
||||
}: JalaliDateTime): string {
|
||||
const { gy, gm, gd } = d2g(j2d(year, month, day));
|
||||
return new Date(
|
||||
Date.UTC(gy, gm - 1, gd, hour, minute) - TEHRAN_OFFSET_MINUTES * MINUTE_MS,
|
||||
).toISOString();
|
||||
}
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
/** A wall-clock moment in Tehran on the Shamsi calendar. Months and days start at 1. */
|
||||
export type JalaliDateTime = {
|
||||
year: number;
|
||||
month: number;
|
||||
day: number;
|
||||
hour: number;
|
||||
minute: number;
|
||||
};
|
||||
Loading…
Reference in New Issue