From db0fa7c86e9f2c66034f087848d44d69eb5ffcb3 Mon Sep 17 00:00:00 2001 From: Amiram_se Date: Fri, 9 Oct 2026 16:11:12 +0400 Subject: [PATCH] feat(island,tools): select, switch, info tip, jalali date helpers Co-Authored-By: Claude Opus 5.5 --- packages/island/src/atoms/select/Select.tsx | 59 ++++++++ packages/island/src/atoms/select/index.ts | 2 + packages/island/src/atoms/select/types.ts | 9 ++ packages/island/src/atoms/switch/Switch.tsx | 58 ++++++++ packages/island/src/atoms/switch/index.ts | 3 + packages/island/src/atoms/switch/types.ts | 16 +++ packages/island/src/atoms/switch/variants.ts | 28 ++++ packages/island/src/index.ts | 10 ++ .../island/src/molecules/info-tip/InfoTip.tsx | 71 ++++++++++ .../island/src/molecules/info-tip/index.ts | 3 + .../island/src/molecules/info-tip/types.ts | 9 ++ .../island/src/molecules/info-tip/variants.ts | 12 ++ packages/tools/src/index.ts | 7 + packages/tools/src/jalali/constants.ts | 12 ++ packages/tools/src/jalali/index.ts | 7 + packages/tools/src/jalali/jalali.ts | 129 ++++++++++++++++++ packages/tools/src/jalali/types.ts | 8 ++ 17 files changed, 443 insertions(+) create mode 100644 packages/island/src/atoms/select/Select.tsx create mode 100644 packages/island/src/atoms/select/index.ts create mode 100644 packages/island/src/atoms/select/types.ts create mode 100644 packages/island/src/atoms/switch/Switch.tsx create mode 100644 packages/island/src/atoms/switch/index.ts create mode 100644 packages/island/src/atoms/switch/types.ts create mode 100644 packages/island/src/atoms/switch/variants.ts create mode 100644 packages/island/src/molecules/info-tip/InfoTip.tsx create mode 100644 packages/island/src/molecules/info-tip/index.ts create mode 100644 packages/island/src/molecules/info-tip/types.ts create mode 100644 packages/island/src/molecules/info-tip/variants.ts create mode 100644 packages/tools/src/jalali/constants.ts create mode 100644 packages/tools/src/jalali/index.ts create mode 100644 packages/tools/src/jalali/jalali.ts create mode 100644 packages/tools/src/jalali/types.ts diff --git a/packages/island/src/atoms/select/Select.tsx b/packages/island/src/atoms/select/Select.tsx new file mode 100644 index 0000000..638b64e --- /dev/null +++ b/packages/island/src/atoms/select/Select.tsx @@ -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 ( + + + {label} + + + + {options.map((option) => ( + + {option.label} + + ))} + + + + + ); +} diff --git a/packages/island/src/atoms/select/index.ts b/packages/island/src/atoms/select/index.ts new file mode 100644 index 0000000..e94e2d4 --- /dev/null +++ b/packages/island/src/atoms/select/index.ts @@ -0,0 +1,2 @@ +export { Select } from './Select'; +export type { SelectOption, SelectProps } from './types'; diff --git a/packages/island/src/atoms/select/types.ts b/packages/island/src/atoms/select/types.ts new file mode 100644 index 0000000..94e0cac --- /dev/null +++ b/packages/island/src/atoms/select/types.ts @@ -0,0 +1,9 @@ +import type { ComponentPropsWithRef, ReactNode } from 'react'; + +export type SelectOption = { value: string; label: ReactNode }; + +export type SelectProps = Omit, 'children'> & { + /** The visible label above the control, such as «روز». */ + label: ReactNode; + options: readonly SelectOption[]; +}; diff --git a/packages/island/src/atoms/switch/Switch.tsx b/packages/island/src/atoms/switch/Switch.tsx new file mode 100644 index 0000000..2ab8f39 --- /dev/null +++ b/packages/island/src/atoms/switch/Switch.tsx @@ -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 ( + { + 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 && ( + + {children} + + )} + + + + + ); +} diff --git a/packages/island/src/atoms/switch/index.ts b/packages/island/src/atoms/switch/index.ts new file mode 100644 index 0000000..9057988 --- /dev/null +++ b/packages/island/src/atoms/switch/index.ts @@ -0,0 +1,3 @@ +export { Switch } from './Switch'; +export type { SwitchProps } from './types'; +export { switchKnobVariants, switchTrackVariants } from './variants'; diff --git a/packages/island/src/atoms/switch/types.ts b/packages/island/src/atoms/switch/types.ts new file mode 100644 index 0000000..590653e --- /dev/null +++ b/packages/island/src/atoms/switch/types.ts @@ -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; +}; diff --git a/packages/island/src/atoms/switch/variants.ts b/packages/island/src/atoms/switch/variants.ts new file mode 100644 index 0000000..ab183df --- /dev/null +++ b/packages/island/src/atoms/switch/variants.ts @@ -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 }, + }, +); diff --git a/packages/island/src/index.ts b/packages/island/src/index.ts index 80ad9ad..02535a7 100644 --- a/packages/island/src/index.ts +++ b/packages/island/src/index.ts @@ -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'; diff --git a/packages/island/src/molecules/info-tip/InfoTip.tsx b/packages/island/src/molecules/info-tip/InfoTip.tsx new file mode 100644 index 0000000..4f2c73e --- /dev/null +++ b/packages/island/src/molecules/info-tip/InfoTip.tsx @@ -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(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 ( + setIsOpen(false)} + > + 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" + > + + + + {children} + + + ); +} diff --git a/packages/island/src/molecules/info-tip/index.ts b/packages/island/src/molecules/info-tip/index.ts new file mode 100644 index 0000000..2ff6d99 --- /dev/null +++ b/packages/island/src/molecules/info-tip/index.ts @@ -0,0 +1,3 @@ +export { InfoTip } from './InfoTip'; +export type { InfoTipProps } from './types'; +export { infoTipVariants } from './variants'; diff --git a/packages/island/src/molecules/info-tip/types.ts b/packages/island/src/molecules/info-tip/types.ts new file mode 100644 index 0000000..42a9034 --- /dev/null +++ b/packages/island/src/molecules/info-tip/types.ts @@ -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; +}; diff --git a/packages/island/src/molecules/info-tip/variants.ts b/packages/island/src/molecules/info-tip/variants.ts new file mode 100644 index 0000000..efa7427 --- /dev/null +++ b/packages/island/src/molecules/info-tip/variants.ts @@ -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 }, + }, +); diff --git a/packages/tools/src/index.ts b/packages/tools/src/index.ts index 717145e..da587a9 100644 --- a/packages/tools/src/index.ts +++ b/packages/tools/src/index.ts @@ -21,3 +21,10 @@ export type { DateInput, ShebaFormatOptions, } from './format'; +export { + fromJalali, + isJalaliLeapYear, + jalaliMonthLength, + toJalali, +} from './jalali'; +export type { JalaliDateTime } from './jalali'; diff --git a/packages/tools/src/jalali/constants.ts b/packages/tools/src/jalali/constants.ts new file mode 100644 index 0000000..2a1c872 --- /dev/null +++ b/packages/tools/src/jalali/constants.ts @@ -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; diff --git a/packages/tools/src/jalali/index.ts b/packages/tools/src/jalali/index.ts new file mode 100644 index 0000000..4dc06c3 --- /dev/null +++ b/packages/tools/src/jalali/index.ts @@ -0,0 +1,7 @@ +export { + fromJalali, + isJalaliLeapYear, + jalaliMonthLength, + toJalali, +} from './jalali'; +export type { JalaliDateTime } from './types'; diff --git a/packages/tools/src/jalali/jalali.ts b/packages/tools/src/jalali/jalali.ts new file mode 100644 index 0000000..d1ea9b3 --- /dev/null +++ b/packages/tools/src/jalali/jalali.ts @@ -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(); +} diff --git a/packages/tools/src/jalali/types.ts b/packages/tools/src/jalali/types.ts new file mode 100644 index 0000000..408cb53 --- /dev/null +++ b/packages/tools/src/jalali/types.ts @@ -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; +};