@42-central/ui-react 0.28.0 → 0.29.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (108) hide show
  1. package/dist/{chunk-I53TMAV2.js → chunk-2RQLPN4U.js} +3 -3
  2. package/dist/{chunk-I53TMAV2.js.map → chunk-2RQLPN4U.js.map} +1 -1
  3. package/dist/{chunk-SOUDKESO.js → chunk-37HYN7PC.js} +3 -3
  4. package/dist/{chunk-SOUDKESO.js.map → chunk-37HYN7PC.js.map} +1 -1
  5. package/dist/{chunk-TGGNSSOJ.js → chunk-5EOMSWNO.js} +4 -4
  6. package/dist/{chunk-TGGNSSOJ.js.map → chunk-5EOMSWNO.js.map} +1 -1
  7. package/dist/{chunk-K3FZHYJL.js → chunk-7FPSQBWB.js} +3 -3
  8. package/dist/{chunk-K3FZHYJL.js.map → chunk-7FPSQBWB.js.map} +1 -1
  9. package/dist/{chunk-WERJ44ZN.js → chunk-DTFSCR3U.js} +3 -3
  10. package/dist/{chunk-WERJ44ZN.js.map → chunk-DTFSCR3U.js.map} +1 -1
  11. package/dist/{chunk-PFZMB3US.js → chunk-FQJNQW3G.js} +3 -3
  12. package/dist/{chunk-PFZMB3US.js.map → chunk-FQJNQW3G.js.map} +1 -1
  13. package/dist/{chunk-ASKQ6N5V.js → chunk-FSPS7TVD.js} +4 -4
  14. package/dist/{chunk-ASKQ6N5V.js.map → chunk-FSPS7TVD.js.map} +1 -1
  15. package/dist/{chunk-GJFCIG34.js → chunk-GEZYGBXY.js} +5 -5
  16. package/dist/{chunk-GJFCIG34.js.map → chunk-GEZYGBXY.js.map} +1 -1
  17. package/dist/{chunk-ULF5LOBS.js → chunk-HMNXNKDA.js} +3 -3
  18. package/dist/{chunk-ULF5LOBS.js.map → chunk-HMNXNKDA.js.map} +1 -1
  19. package/dist/{chunk-NF2PBGD3.js → chunk-IANYMDKH.js} +3 -3
  20. package/dist/{chunk-NF2PBGD3.js.map → chunk-IANYMDKH.js.map} +1 -1
  21. package/dist/{chunk-QEMXXQQO.js → chunk-JKFPRQB2.js} +3 -3
  22. package/dist/{chunk-QEMXXQQO.js.map → chunk-JKFPRQB2.js.map} +1 -1
  23. package/dist/{chunk-P7SH3XWY.js → chunk-MQHD3EJE.js} +3 -3
  24. package/dist/{chunk-P7SH3XWY.js.map → chunk-MQHD3EJE.js.map} +1 -1
  25. package/dist/{chunk-IUK7OJXZ.js → chunk-OBSZ4NE2.js} +5 -4
  26. package/dist/chunk-OBSZ4NE2.js.map +1 -0
  27. package/dist/{chunk-DJ72KP32.js → chunk-OXR7BXZA.js} +3 -3
  28. package/dist/{chunk-DJ72KP32.js.map → chunk-OXR7BXZA.js.map} +1 -1
  29. package/dist/{chunk-LIZWCWED.js → chunk-PQZSTI3R.js} +3 -3
  30. package/dist/{chunk-LIZWCWED.js.map → chunk-PQZSTI3R.js.map} +1 -1
  31. package/dist/{chunk-7BY5I2RB.js → chunk-S5CQRCTF.js} +4 -4
  32. package/dist/{chunk-7BY5I2RB.js.map → chunk-S5CQRCTF.js.map} +1 -1
  33. package/dist/{chunk-BGDVUC3B.js → chunk-TUP7WXQE.js} +3 -3
  34. package/dist/{chunk-BGDVUC3B.js.map → chunk-TUP7WXQE.js.map} +1 -1
  35. package/dist/{chunk-KYVXPSNW.js → chunk-V2QZG4IT.js} +3 -3
  36. package/dist/{chunk-KYVXPSNW.js.map → chunk-V2QZG4IT.js.map} +1 -1
  37. package/dist/components/alert/index.js +1 -1
  38. package/dist/components/ambient-background/index.js +1 -1
  39. package/dist/components/app-shell/index.js +3 -3
  40. package/dist/components/avatar/index.js +2 -2
  41. package/dist/components/calendar/index.js +3 -3
  42. package/dist/components/calendar-heatmap/index.js +2 -2
  43. package/dist/components/camera/index.js +3 -3
  44. package/dist/components/carousel/index.js +2 -2
  45. package/dist/components/code-block/index.js +1 -1
  46. package/dist/components/code-block/static.js.map +1 -1
  47. package/dist/components/compare/index.js +2 -2
  48. package/dist/components/data-table/index.js +10 -10
  49. package/dist/components/date-picker/index.js +7 -7
  50. package/dist/components/drawer/index.js +3 -3
  51. package/dist/components/file-item/index.js +4 -4
  52. package/dist/components/file-preview/index.js +3 -3
  53. package/dist/components/file-upload/index.js +4 -4
  54. package/dist/components/gallery/index.js +2 -2
  55. package/dist/components/image-cropper/index.js +4 -4
  56. package/dist/components/indicator/index.js +1 -1
  57. package/dist/components/kbd/index.js +3 -3
  58. package/dist/components/lightbox/index.js +4 -4
  59. package/dist/components/menu/index.js +2 -2
  60. package/dist/components/modal/index.js +3 -3
  61. package/dist/components/multi-select/index.js +3 -3
  62. package/dist/components/overlays/index.js +4 -4
  63. package/dist/components/password-input/index.js +1 -1
  64. package/dist/components/phone-input/flag.d.ts +9 -0
  65. package/dist/components/phone-input/flag.d.ts.map +1 -0
  66. package/dist/components/phone-input/index.d.ts +4 -0
  67. package/dist/components/phone-input/index.d.ts.map +1 -0
  68. package/dist/components/phone-input/index.js +503 -0
  69. package/dist/components/phone-input/index.js.map +1 -0
  70. package/dist/components/phone-input/phone-input.d.ts +125 -0
  71. package/dist/components/phone-input/phone-input.d.ts.map +1 -0
  72. package/dist/components/phone-input/phone.d.ts +117 -0
  73. package/dist/components/phone-input/phone.d.ts.map +1 -0
  74. package/dist/components/photo-capture/index.js +5 -5
  75. package/dist/components/pills-input/index.js +3 -3
  76. package/dist/components/schedule/index.js +3 -3
  77. package/dist/components/segment-group/index.js +3 -3
  78. package/dist/components/spotlight/index.js +5 -5
  79. package/dist/components/tabs/index.js +3 -3
  80. package/dist/components/tags-input/index.js +3 -3
  81. package/dist/components/time-input/index.js +4 -4
  82. package/dist/components/timeline/index.d.ts +1 -1
  83. package/dist/components/timeline/index.d.ts.map +1 -1
  84. package/dist/components/timeline/index.js +48 -12
  85. package/dist/components/timeline/index.js.map +1 -1
  86. package/dist/components/timeline/timeline.d.ts +8 -3
  87. package/dist/components/timeline/timeline.d.ts.map +1 -1
  88. package/dist/components/tree-multi-select/index.js +3 -3
  89. package/dist/index.js +4 -4
  90. package/dist/lib/dev-warn.d.ts +1 -0
  91. package/dist/lib/dev-warn.d.ts.map +1 -1
  92. package/dist/lib/intl.d.ts +5 -0
  93. package/dist/lib/intl.d.ts.map +1 -1
  94. package/package.json +3 -1
  95. package/src/as-child.contract.test.tsx +2 -0
  96. package/src/components/phone-input/flag.tsx +86 -0
  97. package/src/components/phone-input/index.ts +14 -0
  98. package/src/components/phone-input/phone-input.test.tsx +285 -0
  99. package/src/components/phone-input/phone-input.tsx +527 -0
  100. package/src/components/phone-input/phone.test.ts +187 -0
  101. package/src/components/phone-input/phone.ts +298 -0
  102. package/src/components/timeline/index.ts +1 -0
  103. package/src/components/timeline/timeline.test.tsx +127 -4
  104. package/src/components/timeline/timeline.tsx +56 -12
  105. package/src/lib/dev-warn.ts +1 -1
  106. package/src/lib/intl-direction.test.tsx +23 -0
  107. package/src/lib/intl.tsx +8 -2
  108. package/dist/chunk-IUK7OJXZ.js.map +0 -1
@@ -0,0 +1,187 @@
1
+ import { describe, expect, it } from "vitest"
2
+ import {
3
+ caretAfter,
4
+ countryOfLocale,
5
+ deleteAcrossSeparator,
6
+ formatPhone,
7
+ parsePhone,
8
+ releaseCountry,
9
+ significantBefore,
10
+ switchCountry,
11
+ } from "./phone"
12
+
13
+ describe("formatPhone", () => {
14
+ it.each([
15
+ ["FR", "0612345678", "06 12 34 56 78", "+33612345678"],
16
+ ["US", "2015550123", "(201) 555-0123", "+12015550123"],
17
+ ["GB", "02079460958", "020 7946 0958", "+442079460958"],
18
+ ] as const)("formats a %s national number and reports E.164", (country, raw, text, value) => {
19
+ expect(formatPhone(raw, country)).toEqual({ text, country, value, isValid: true, consumed: 0 })
20
+ })
21
+
22
+ it("folds pasted separators into the country's own grouping", () => {
23
+ expect(formatPhone("06.12-34 (56) 78", "FR").text).toBe("06 12 34 56 78")
24
+ })
25
+
26
+ it("reports a partial number as invalid, with its partial E.164", () => {
27
+ expect(formatPhone("0612", "FR")).toMatchObject({ value: "+33612", isValid: false })
28
+ })
29
+
30
+ it("is empty and null with nothing typed", () => {
31
+ expect(formatPhone(" -", "FR")).toEqual({
32
+ text: "",
33
+ country: "FR",
34
+ value: null,
35
+ isValid: false,
36
+ consumed: 0,
37
+ })
38
+ })
39
+
40
+ it("collapses a pasted international number's code into its country", () => {
41
+ expect(formatPhone("+44 20 7946 0958", "FR")).toEqual({
42
+ text: "20 7946 0958",
43
+ country: "GB",
44
+ value: "+442079460958",
45
+ isValid: true,
46
+ consumed: 3,
47
+ })
48
+ })
49
+
50
+ it("collapses the code the moment it is complete, typed with the caret at its end", () => {
51
+ expect(formatPhone("+", null, 1)).toMatchObject({ text: "+", country: null, consumed: 0 })
52
+ expect(formatPhone("+3", null, 2)).toMatchObject({ text: "+3", country: null, consumed: 0 })
53
+ expect(formatPhone("+33", null, 3)).toMatchObject({ text: "", country: "FR", consumed: 3 })
54
+ })
55
+
56
+ it("resolves a shared code to its main country, or the current one when it shares it", () => {
57
+ expect(formatPhone("+44", null, 3).country).toBe("GB")
58
+ expect(formatPhone("+1", "FR", 2).country).toBe("US")
59
+ expect(formatPhone("+1", "CA", 2).country).toBe("CA")
60
+ })
61
+
62
+ it("refines a resolved shared code from the national digits", () => {
63
+ expect(formatPhone("+44 7797 123456", null).country).toBe("JE")
64
+ expect(formatPhone("2045550123", "US", undefined, true).country).toBe("CA")
65
+ })
66
+
67
+ it("leaves a chosen country alone", () => {
68
+ expect(formatPhone("2045550123", "US")).toMatchObject({
69
+ country: "US",
70
+ value: "+12045550123",
71
+ })
72
+ })
73
+
74
+ it("keeps a code being edited as text instead of collapsing it", () => {
75
+ // `+33 6 12` with its second 3 deleted: `+36` is Hungary's code, but the caret is inside it.
76
+ expect(formatPhone("+3612", null, 2)).toMatchObject({
77
+ text: "+36 1 2",
78
+ country: "HU",
79
+ consumed: 0,
80
+ })
81
+ // Finishing the code (typing the 3 back) collapses it again.
82
+ expect(formatPhone("+33612", null, 3)).toMatchObject({ text: "6 12", country: "FR" })
83
+ })
84
+
85
+ it("has no number without a country unless it is international", () => {
86
+ expect(formatPhone("0612", null).value).toBeNull()
87
+ })
88
+ })
89
+
90
+ describe("national grouping", () => {
91
+ it("groups a collapsed number with no trunk prefix", () => {
92
+ expect(formatPhone("612345678", "FR").text).toBe("6 12 34 56 78")
93
+ expect(formatPhone("2079460958", "GB").text).toBe("20 7946 0958")
94
+ })
95
+
96
+ it("keeps the national grouping where it applies", () => {
97
+ expect(formatPhone("0612345678", "FR").text).toBe("06 12 34 56 78")
98
+ expect(formatPhone("2015550123", "US").text).toBe("(201) 555-0123")
99
+ })
100
+ })
101
+
102
+ describe("parsePhone", () => {
103
+ it("shows a complete E.164 value in its national format", () => {
104
+ expect(parsePhone("+33612345678", null)).toEqual({
105
+ text: "06 12 34 56 78",
106
+ country: "FR",
107
+ value: "+33612345678",
108
+ isValid: true,
109
+ })
110
+ })
111
+
112
+ it("keeps the fallback country for an empty value", () => {
113
+ expect(parsePhone(null, "BE")).toEqual({ text: "", country: "BE", value: null, isValid: false })
114
+ })
115
+
116
+ it("collapses the code of a value too short to be national", () => {
117
+ expect(parsePhone("+336", null)).toEqual({
118
+ text: "6",
119
+ country: "FR",
120
+ value: "+336",
121
+ isValid: false,
122
+ })
123
+ })
124
+ })
125
+
126
+ describe("switchCountry", () => {
127
+ it("drops the calling code of an international number", () => {
128
+ expect(switchCountry("+44 20 7946 0958", "FR").text).not.toMatch(/^\+/)
129
+ })
130
+
131
+ it("re-formats a national number's digits under the new country", () => {
132
+ expect(switchCountry("2015550123", "US").text).toBe("(201) 555-0123")
133
+ })
134
+ })
135
+
136
+ describe("releaseCountry", () => {
137
+ it("is the inverse of typing the code's last digit when there is no number yet", () => {
138
+ expect(releaseCountry("", "FR")).toEqual({
139
+ next: { text: "+3", country: null, value: null, isValid: false },
140
+ caret: 2,
141
+ })
142
+ expect(releaseCountry("", "US").next.text).toBe("+")
143
+ })
144
+
145
+ it("brings the code back whole, without deleting, when a number follows", () => {
146
+ expect(releaseCountry("6 12", "FR")).toEqual({
147
+ next: { text: "+33 6 12", country: null, value: "+33612", isValid: false },
148
+ caret: 3,
149
+ })
150
+ })
151
+ })
152
+
153
+ describe("caret helpers", () => {
154
+ it("counts digits and a leading plus", () => {
155
+ expect(significantBefore("+33 6 12", 5)).toBe(4)
156
+ expect(significantBefore("06 12", 3)).toBe(2)
157
+ })
158
+
159
+ it("places the caret right after the n-th significant character", () => {
160
+ expect(caretAfter("06 12 34", 3)).toBe(4)
161
+ expect(caretAfter("06 12 34", 0)).toBe(0)
162
+ expect(caretAfter("06 12 34", 99)).toBe(8)
163
+ })
164
+
165
+ it("backspace over a separator removes the digit before it", () => {
166
+ expect(deleteAcrossSeparator("06 12", 3, "backward")).toEqual({ raw: "0 12", count: 1 })
167
+ })
168
+
169
+ it("delete over a separator removes the digit after it", () => {
170
+ expect(deleteAcrossSeparator("06 12", 2, "forward")).toEqual({ raw: "06 2", count: 2 })
171
+ })
172
+
173
+ it("leaves a plain digit deletion to the browser", () => {
174
+ expect(deleteAcrossSeparator("06 12", 2, "backward")).toBeNull()
175
+ })
176
+ })
177
+
178
+ describe("countryOfLocale", () => {
179
+ it("reads the region, filling in a bare language", () => {
180
+ expect(countryOfLocale("en-GB")).toBe("GB")
181
+ expect(countryOfLocale("fr")).toBe("FR")
182
+ })
183
+
184
+ it("is null for a region libphonenumber doesn't know", () => {
185
+ expect(countryOfLocale("en-001")).toBeNull()
186
+ })
187
+ })
@@ -0,0 +1,298 @@
1
+ import {
2
+ AsYouType,
3
+ type CountryCode,
4
+ getCountries,
5
+ getCountryCallingCode,
6
+ getExampleNumber,
7
+ parsePhoneNumberFromString,
8
+ } from "libphonenumber-js/min"
9
+ import metadata from "libphonenumber-js/min/metadata"
10
+ import examples from "libphonenumber-js/mobile/examples"
11
+
12
+ /**
13
+ * PhoneInput's engine, with nothing visual in it: libphonenumber-js formats
14
+ * as you type and produces the E.164 value, and the caret helpers below keep
15
+ * the cursor where the user left it while the formatting moves characters
16
+ * around it.
17
+ *
18
+ * The dial code behaves like a token in front of the number. Typing a complete
19
+ * code (`+33`) moves it out of the text and into the country trigger. Backspace
20
+ * at the very start of the text pulls it back out as editable characters
21
+ * (`releaseCountry`). Whenever a country is selected, the text is national.
22
+ */
23
+
24
+ export type { CountryCode }
25
+
26
+ /** Every country libphonenumber-js has metadata for. */
27
+ export const PHONE_COUNTRIES: readonly CountryCode[] = getCountries()
28
+
29
+ /** `"33"` for `"FR"`. */
30
+ export const callingCodeOf = (country: CountryCode): string => getCountryCallingCode(country)
31
+
32
+ /**
33
+ * A real-looking mobile number for `country`, as its people write it (`06 12 34 56 78`,
34
+ * `07400 123456`, `(201) 555-0123`), for the placeholder. libphonenumber's example numbers
35
+ * weigh ~1.4 KB gzipped for every country.
36
+ */
37
+ export const exampleOf = (country: CountryCode): string | undefined =>
38
+ getExampleNumber(country, examples)?.formatNational()
39
+
40
+ /**
41
+ * The main country of a calling code shared by several (`44` is GB, GG, IM and JE; `1` is the
42
+ * US, Canada and the Caribbean). libphonenumber's metadata lists the main one first. It's the
43
+ * same metadata module the `min` build already bundles, so this costs nothing extra.
44
+ */
45
+ const mainCountryOf = (callingCode: string): CountryCode | null =>
46
+ (metadata.country_calling_codes[callingCode]?.[0] as CountryCode | undefined) ?? null
47
+
48
+ /** What the field holds after a keystroke. */
49
+ export type PhoneState = {
50
+ /** The formatted text shown in the `<input>`. National whenever a country is selected. */
51
+ text: string
52
+ /** The selected country — resolved from a typed `+…` code, else the one the user picked. */
53
+ country: CountryCode | null
54
+ /** E.164 (`"+33612345678"`), or `null` while there's no number to speak of. */
55
+ value: string | null
56
+ /** Whether the number is a complete, valid number for its country. */
57
+ isValid: boolean
58
+ }
59
+
60
+ /** A keystroke's result, plus how many caret-relevant characters (the `+` and the code's
61
+ * digits) moved out of the text and into the trigger, so the caret can follow. */
62
+ export type FormattedPhone = PhoneState & { consumed: number }
63
+
64
+ const startsInternational = (text: string) => /^\s*\+/.test(text)
65
+
66
+ /**
67
+ * Which country national `digits` belong to, among those sharing `country`'s calling code:
68
+ * `204 555 0123` under the US is Canadian. Stays on `country` while the digits can't tell yet.
69
+ */
70
+ const refineCountry = (digits: string, country: CountryCode): CountryCode => {
71
+ const found = digits ? parsePhoneNumberFromString(digits, country)?.country : undefined
72
+ return found && callingCodeOf(found) === callingCodeOf(country) ? found : country
73
+ }
74
+
75
+ /**
76
+ * Group national `digits` for display. libphonenumber's national grouping only kicks in when
77
+ * the digits start with the country's trunk prefix (`06 12 …` in France, `020 …` in the UK). A
78
+ * number whose code just collapsed into the trigger has no trunk prefix (`6 12 …`), and would
79
+ * come out as one unbroken run. Then the international grouping is used, minus its `+code`.
80
+ * NANP numbers, which have no trunk prefix in front, keep their national form,
81
+ * `(201) 555-0123`.
82
+ */
83
+ const groupNational = (digits: string, country: CountryCode, national: string): string => {
84
+ if (/\D/.test(national)) return national
85
+ const code = callingCodeOf(country)
86
+ const international = new AsYouType().input(`+${code}${digits}`)
87
+ return international.startsWith(`+${code}`)
88
+ ? international.slice(code.length + 1).trim()
89
+ : national
90
+ }
91
+
92
+ /** National `digits` under `country` (or bare digits with none, which make no number).
93
+ * `refine` lets the digits move the country within its calling code (see `formatPhone`). */
94
+ const formatNational = (
95
+ digits: string,
96
+ country: CountryCode | null,
97
+ refine: boolean,
98
+ ): PhoneState => {
99
+ if (digits === "") return { text: "", country, value: null, isValid: false }
100
+ const resolved = country && (refine ? refineCountry(digits, country) : country)
101
+ const typer = new AsYouType(resolved ?? undefined)
102
+ const national = typer.input(digits)
103
+ const text = resolved ? groupNational(digits, resolved, national) : national
104
+ return {
105
+ text,
106
+ country: resolved,
107
+ value: resolved ? (typer.getNumber()?.number ?? null) : null,
108
+ isValid: typer.isValid(),
109
+ }
110
+ }
111
+
112
+ /**
113
+ * Format `raw` as typed under `country`. Only digits and a leading `+` survive,
114
+ * so pasted spaces, dashes, dots and parentheses all fold into the country's
115
+ * own grouping.
116
+ *
117
+ * A national number keeps the selected country. With `refine`, the digits move it
118
+ * within its calling code (`204 …` under the US becomes Canada): that's for a
119
+ * country resolved from a typed code, which the user never chose. A country they
120
+ * picked stays put; the E.164 value is the same either way. An international one
121
+ * collapses its code
122
+ * into the country as soon as the code is complete. Calling codes are
123
+ * prefix-free, so `+33` can't be the start of a longer one. A shared code
124
+ * resolves to the current country if it shares it, else to the code's main
125
+ * country, and the national digits refine it.
126
+ *
127
+ * `caret` is the number of caret-relevant characters before the caret. The code
128
+ * only collapses when the caret is at or past its end. With the caret inside the
129
+ * code, the user is editing it: `+3|6 12` after deleting a `3` is a complete
130
+ * Hungarian code, and collapsing it would jump to a country they never typed.
131
+ * It stays international text until they finish the code. Omit `caret` (a
132
+ * paste, a value) to collapse unconditionally.
133
+ */
134
+ export const formatPhone = (
135
+ raw: string,
136
+ country: CountryCode | null,
137
+ caret?: number,
138
+ refine = false,
139
+ ): FormattedPhone => {
140
+ const digits = raw.replace(/\D/g, "")
141
+ if (!startsInternational(raw)) return { ...formatNational(digits, country, refine), consumed: 0 }
142
+
143
+ const typer = new AsYouType()
144
+ const text = typer.input(`+${digits}`)
145
+ const callingCode = typer.getCallingCode()
146
+ const shared =
147
+ callingCode && country && callingCodeOf(country) === callingCode
148
+ ? country
149
+ : callingCode
150
+ ? mainCountryOf(callingCode)
151
+ : null
152
+ const codeEnd = 1 + (callingCode?.length ?? 0)
153
+ if (callingCode && shared && (caret === undefined || caret >= codeEnd)) {
154
+ return {
155
+ ...formatNational(digits.slice(callingCode.length), shared, true),
156
+ consumed: codeEnd,
157
+ }
158
+ }
159
+ // Still typing (or editing) the code: international text, with whatever country the digits
160
+ // so far point to, for the flag.
161
+ return {
162
+ text,
163
+ country: typer.getCountry() ?? shared,
164
+ value: typer.getNumber()?.number ?? null,
165
+ isValid: typer.isValid(),
166
+ consumed: 0,
167
+ }
168
+ }
169
+
170
+ /**
171
+ * Backspace at the very start of the text, with a country selected: the dial code comes back
172
+ * out of the trigger as editable text, and the country is unselected.
173
+ *
174
+ * - With no number yet, it's the exact inverse of the keystroke that created the token: `+33`
175
+ * collapsed to France, so Backspace leaves `+3`.
176
+ * - With a number after it, the code comes back whole (`+33| 6 12`, caret after the code)
177
+ * without deleting anything. Deleting its last digit would leave `+36 12`, a Hungarian number
178
+ * the user never typed.
179
+ *
180
+ * Returns the new state and the caret position, in caret-relevant characters.
181
+ */
182
+ export const releaseCountry = (
183
+ text: string,
184
+ country: CountryCode,
185
+ ): { next: PhoneState; caret: number } => {
186
+ const code = callingCodeOf(country)
187
+ const digits = text.replace(/\D/g, "")
188
+ if (digits === "") {
189
+ const raw = `+${code.slice(0, -1)}`
190
+ const { consumed: _, ...next } = formatPhone(raw, null, raw.length)
191
+ return { next, caret: raw.length }
192
+ }
193
+ const typer = new AsYouType()
194
+ return {
195
+ next: {
196
+ text: typer.input(`+${code}${digits}`),
197
+ country: null,
198
+ value: typer.getNumber()?.number ?? null,
199
+ isValid: typer.isValid(),
200
+ },
201
+ caret: 1 + code.length,
202
+ }
203
+ }
204
+
205
+ /**
206
+ * The display state for an E.164 `value` handed in from outside (initial or
207
+ * controlled): national formatting once the number is possible for a known
208
+ * country, the collapsed code and national digits otherwise. A `null` value
209
+ * keeps `fallback`.
210
+ */
211
+ export const parsePhone = (value: string | null, fallback: CountryCode | null): PhoneState => {
212
+ if (!value) return { text: "", country: fallback, value: null, isValid: false }
213
+ const parsed = parsePhoneNumberFromString(value)
214
+ if (parsed?.country && parsed.isPossible()) {
215
+ return {
216
+ text: parsed.formatNational(),
217
+ country: parsed.country,
218
+ value: parsed.number,
219
+ isValid: parsed.isValid(),
220
+ }
221
+ }
222
+ const { consumed: _, ...state } = formatPhone(value, fallback)
223
+ return state
224
+ }
225
+
226
+ /**
227
+ * Re-home the current text under a newly picked country: international text
228
+ * (a code still being typed) drops its calling code and keeps its national
229
+ * digits, a national one keeps its digits as they are.
230
+ */
231
+ export const switchCountry = (text: string, country: CountryCode | null): PhoneState => {
232
+ if (!startsInternational(text)) return formatNational(text.replace(/\D/g, ""), country, false)
233
+ const typer = new AsYouType()
234
+ typer.input(text)
235
+ const callingCode = typer.getCallingCode() ?? ""
236
+ return formatNational(text.replace(/\D/g, "").slice(callingCode.length), country, false)
237
+ }
238
+
239
+ /** How many caret-relevant characters (digits, and a leading `+`) sit before `end`. */
240
+ export const significantBefore = (text: string, end: number): number => {
241
+ const head = text.slice(0, end)
242
+ return (head.match(/\d/g)?.length ?? 0) + (startsInternational(head) ? 1 : 0)
243
+ }
244
+
245
+ const isSignificant = (text: string, index: number) =>
246
+ /\d/.test(text.charAt(index)) ||
247
+ (text.charAt(index) === "+" && startsInternational(text.slice(0, index + 1)))
248
+
249
+ /**
250
+ * The caret position in `text` right after its `count`-th significant
251
+ * character — where a caret that had `count` digits before it belongs once
252
+ * the text is reformatted.
253
+ */
254
+ export const caretAfter = (text: string, count: number): number => {
255
+ if (count <= 0) return 0
256
+ const index = [...text].findIndex((_, i) => significantBefore(text, i + 1) === count)
257
+ return index === -1 ? text.length : index + 1
258
+ }
259
+
260
+ /**
261
+ * Backspace / Delete on a formatting character (a space, a parenthesis, a
262
+ * dash) would be undone by the next format and look like a dead key. Instead
263
+ * it removes the nearest digit in that direction, the way every phone field
264
+ * that formats as you type behaves. Returns `null` when the native edit is
265
+ * fine as it is: a selection, or a digit next to the caret.
266
+ */
267
+ export const deleteAcrossSeparator = (
268
+ text: string,
269
+ caret: number,
270
+ direction: "backward" | "forward",
271
+ ): { raw: string; count: number } | null => {
272
+ const neighbour = direction === "backward" ? caret - 1 : caret
273
+ if (neighbour < 0 || neighbour >= text.length || isSignificant(text, neighbour)) return null
274
+ const indices = Array.from(text, (_, i) => i)
275
+ const target =
276
+ direction === "backward"
277
+ ? indices.filter((i) => i < caret && isSignificant(text, i)).at(-1)
278
+ : indices.find((i) => i >= caret && isSignificant(text, i))
279
+ if (target === undefined) return null
280
+ return {
281
+ raw: text.slice(0, target) + text.slice(target + 1),
282
+ count: significantBefore(text, direction === "backward" ? target : caret),
283
+ }
284
+ }
285
+
286
+ /**
287
+ * The country a locale implies (`"fr"` → `FR`, `"en-GB"` → `GB`), when
288
+ * libphonenumber knows it. `Intl.Locale#maximize` fills in the likely region
289
+ * of a bare language tag.
290
+ */
291
+ export const countryOfLocale = (locale: string): CountryCode | null => {
292
+ try {
293
+ const region = new Intl.Locale(locale).maximize().region
294
+ return PHONE_COUNTRIES.find((country) => country === region) ?? null
295
+ } catch {
296
+ return null
297
+ }
298
+ }
@@ -1,5 +1,6 @@
1
1
  export {
2
2
  Timeline,
3
+ type TimelineBulletRadius,
3
4
  type TimelineBulletVariant,
4
5
  type TimelineContentProps,
5
6
  type TimelineItemProps,
@@ -578,9 +578,15 @@ describe("Timeline", () => {
578
578
  expect(bullet.className).not.toContain("ring-(--c-solid)")
579
579
  })
580
580
 
581
- it("forces bullet icons to white in dark mode on the colored variants, not default/gradient", () => {
581
+ it("forces bullet icons to white in dark mode on the unfilled variants only", () => {
582
582
  const { container } = render(
583
583
  <Timeline>
584
+ <Timeline.Item color="green" variant="outline" bullet={<span>✓</span>}>
585
+ <Timeline.Content>x</Timeline.Content>
586
+ </Timeline.Item>
587
+ <Timeline.Item color="green" variant="dashed" bullet={<span>✓</span>}>
588
+ <Timeline.Content>x</Timeline.Content>
589
+ </Timeline.Item>
584
590
  <Timeline.Item color="green" variant="filled" bullet={<span>✓</span>}>
585
591
  <Timeline.Content>x</Timeline.Content>
586
592
  </Timeline.Item>
@@ -589,10 +595,127 @@ describe("Timeline", () => {
589
595
  </Timeline.Item>
590
596
  </Timeline>,
591
597
  )
592
- const bullets = container.querySelectorAll('[data-part="bullet"]')
593
- expect((bullets[0] as HTMLElement).className).toContain("dark:[&_svg]:text-white")
598
+ const bullets = [...container.querySelectorAll<HTMLElement>('[data-part="bullet"]')] as [
599
+ HTMLElement,
600
+ HTMLElement,
601
+ HTMLElement,
602
+ HTMLElement,
603
+ ]
604
+ // No fill: the icon sits on the dark page.
605
+ expect(bullets[0].className).toContain("dark:[&_svg]:text-white")
606
+ expect(bullets[1].className).toContain("dark:[&_svg]:text-white")
607
+ // A fill: the icon takes the palette's on-solid colour, like the text in it.
608
+ expect(bullets[2].className).not.toContain("dark:[&_svg]:text-white")
609
+ expect(bullets[2].className).toContain("text-(--c-on-solid)")
594
610
  // default inverts to a *light* dark-mode surface — forcing white would hide the icon.
595
- expect((bullets[1] as HTMLElement).className).not.toContain("dark:[&_svg]:text-white")
611
+ expect(bullets[3].className).not.toContain("dark:[&_svg]:text-white")
612
+ })
613
+
614
+ it("draws a light bullet as a tile of the palette and a subtle one bare", () => {
615
+ const { container } = render(
616
+ <Timeline>
617
+ <Timeline.Item
618
+ color="green"
619
+ variant="light"
620
+ radius="md"
621
+ bullet={<svg aria-hidden="true" />}
622
+ >
623
+ <Timeline.Content>x</Timeline.Content>
624
+ </Timeline.Item>
625
+ <Timeline.Item color="purple" variant="subtle" bullet={<svg aria-hidden="true" />}>
626
+ <Timeline.Content>x</Timeline.Content>
627
+ </Timeline.Item>
628
+ </Timeline>,
629
+ )
630
+ const [light, subtle] = [
631
+ ...container.querySelectorAll<HTMLElement>('[data-part="bullet"]'),
632
+ ] as [HTMLElement, HTMLElement]
633
+ expect(light.className).toContain("bg-(--c-soft)")
634
+ expect(light.className).toContain("border-(--c-solid)/40")
635
+ expect(light.className).toContain("text-(--c-text)")
636
+ expect(light.className).toContain("rounded-md")
637
+ expect(light.className).not.toContain("rounded-full")
638
+ // Its icon keeps the palette's text colour, in either theme.
639
+ expect(light.className).not.toContain("dark:[&_svg]:text-white")
640
+ // Bare: no surface, no ring, a dot's corners by default.
641
+ expect(subtle.className).toContain("text-(--c-text)")
642
+ expect(subtle.className).not.toMatch(/\bbg-|\bborder\b/)
643
+ expect(subtle.className).toContain("rounded-full")
644
+ // Its color still reaches the line.
645
+ expect(subtle.getAttribute("data-color")).toBe("purple")
646
+ })
647
+
648
+ it("lets a subtle bullet show its content whole, and clips the others", () => {
649
+ const { container } = render(
650
+ <Timeline>
651
+ <Timeline.Item variant="subtle" bullet={<span>M3</span>}>
652
+ <Timeline.Content>x</Timeline.Content>
653
+ </Timeline.Item>
654
+ <Timeline.Item variant="light" bullet={<span>✓</span>}>
655
+ <Timeline.Content>x</Timeline.Content>
656
+ </Timeline.Item>
657
+ </Timeline>,
658
+ )
659
+ const [subtle, light] = [
660
+ ...container.querySelectorAll<HTMLElement>('[data-part="bullet"]'),
661
+ ] as [HTMLElement, HTMLElement]
662
+ expect(subtle.className).toContain("overflow-visible")
663
+ expect(subtle.className).not.toContain("overflow-hidden")
664
+ // Centred even when wider than the bullet, not pushed to the end.
665
+ expect(subtle.className).toContain("[place-items:unsafe_center]")
666
+ expect(subtle.className).toContain("grid-cols-[100%]")
667
+ expect(light.className).toContain("overflow-hidden")
668
+ })
669
+
670
+ it("dims a pending light bullet through its soft and text slots", () => {
671
+ const { container } = render(
672
+ <Timeline active={0}>
673
+ <Timeline.Item color="green" variant="light">
674
+ <Timeline.Content>now</Timeline.Content>
675
+ </Timeline.Item>
676
+ <Timeline.Item color="green" variant="light">
677
+ <Timeline.Content>later</Timeline.Content>
678
+ </Timeline.Item>
679
+ </Timeline>,
680
+ )
681
+ const pending = container.querySelectorAll<HTMLElement>(
682
+ '[data-part="bullet"]',
683
+ )[1] as HTMLElement
684
+ expect(pending.className).toContain("[--c-soft:var(--c-soft-disabled)]")
685
+ expect(pending.className).toContain("[--c-text:var(--c-text-disabled)]")
686
+ })
687
+
688
+ it("keeps radius off the list item", () => {
689
+ const { container } = render(
690
+ <Timeline>
691
+ <Timeline.Item radius="sm">
692
+ <Timeline.Content>x</Timeline.Content>
693
+ </Timeline.Item>
694
+ </Timeline>,
695
+ )
696
+ expect(container.querySelector("li")?.hasAttribute("radius")).toBe(false)
697
+ })
698
+
699
+ it("sizes bullet and label icons with zero-specificity rules, so a size class on the icon wins", () => {
700
+ const { container } = render(
701
+ <Timeline>
702
+ <Timeline.Item bullet={<svg aria-hidden="true" />}>
703
+ <Timeline.Label>
704
+ <svg aria-hidden="true" />
705
+ Mar 1
706
+ </Timeline.Label>
707
+ <Timeline.Content>x</Timeline.Content>
708
+ </Timeline.Item>
709
+ </Timeline>,
710
+ )
711
+ for (const part of ["bullet", "label"]) {
712
+ const classes = [
713
+ ...(container.querySelector(`[data-part="${part}"]`) as HTMLElement).classList,
714
+ ]
715
+ expect(classes.some((c) => c.startsWith("[:where(&)_svg]:size-"))).toBe(true)
716
+ // A bare `[&_svg]` size rule would outrank a size class on the icon itself.
717
+ expect(classes.some((c) => /^\[&_svg\]:(size|p)-/.test(c))).toBe(false)
718
+ }
596
719
  })
597
720
 
598
721
  it("drops the ring/gap for a flat, fully filled disc once a bullet icon is present", () => {