From 245083cc4d7ed6280f6d2d509df69bced5640201 Mon Sep 17 00:00:00 2001 From: Pistonight Date: Mon, 25 Nov 2024 23:19:38 -0800 Subject: [PATCH 1/4] bump deps and update pref package --- deno.jsonc | 2 +- package.json | 8 ++++---- pref/dark.ts | 14 ++++++++++++++ pref/locale.ts | 48 ++++++++++++++++++++++++++++++++++++++++------ react/deno.jsonc | 4 ++-- react/package.json | 4 ++-- 6 files changed, 65 insertions(+), 15 deletions(-) diff --git a/deno.jsonc b/deno.jsonc index 25add2c..c6cc9f3 100644 --- a/deno.jsonc +++ b/deno.jsonc @@ -1,6 +1,6 @@ { "name": "@pistonite/pure", - "version": "0.0.10", + "version": "0.0.11", "exports": { "./fs": "./fs/index.ts", "./log": "./log/index.ts", diff --git a/package.json b/package.json index a22087d..37e1b96 100644 --- a/package.json +++ b/package.json @@ -1,13 +1,13 @@ { "dependencies": { - "denque": "^2.1.0", - "file-saver": "^2.0.5" + "denque": "2.1.0", + "file-saver": "2.0.5" }, "devDependencies": { "@types/file-saver": "^2.0.7", - "jsr": "^0.12.4", + "jsr": "^0.13.2", "prettier": "^3.3.3", - "typescript": "^5.5.4" + "typescript": "^5.7.2" }, "prettier": { "tabWidth": 4, diff --git a/pref/dark.ts b/pref/dark.ts index f9ea36f..2b0e669 100644 --- a/pref/dark.ts +++ b/pref/dark.ts @@ -120,6 +120,20 @@ export const initDark = (options: DarkOptions = {}): void => { } }; +/** + * Clears the persisted dark mode preference + * + * If you are doing this, you should probably call `setDark` + * with `prefersDarkMode()` or some initial value immediately before this, + * so the current dark mode is set to user's preferred mode. + * + * Note if `persist` is `true` when initializing, + * subsequence `setDark` calls will still persist the value. + */ +export const clearPersistedDarkPerference = (): void => { + localStorage.removeItem(KEY); +}; + /** * Gets the current value of dark mode */ diff --git a/pref/locale.ts b/pref/locale.ts index 101f6af..75c39fd 100644 --- a/pref/locale.ts +++ b/pref/locale.ts @@ -10,7 +10,7 @@ * * initLocale({ * // required - * supported: ["en", "zh-CN", "zh-TW"], + * supported: ["en", "zh-CN", "zh-TW"] as const, * default: "en", * * // optional @@ -56,8 +56,9 @@ const KEY = "Pure.Locale"; -let supportedLocales: string[] = []; +let supportedLocales: readonly string[] = []; let locale: string = ""; +let defaultLocale: string = ""; const subscribers: ((locale: string) => void)[] = []; /** @@ -82,15 +83,17 @@ export type LocaleOptions = { * List of supported locale or languages. * These can be full locale strings like "en-US" or just languages like "en" */ - supported: TLocale[]; + supported: readonly TLocale[]; /** - * The default locale if the user's preferred locale is not supported + * The default locale if the user's preferred locale is not supported. + * This must be one of the items in `supported`. */ default: TLocale; /** * Initial value for locale * - * If not set, it will default to calling `getPreferredLocale()`. + * If not set, it will default to calling `getPreferredLocale()`, + * which is based on the browser's language settings. * * If `persist` is `true`, it will also check the value from localStorage * @@ -116,6 +119,7 @@ export const initLocale = ( _locale = convertToSupportedLocale(getPreferredLocale()) || options.default; } + defaultLocale = options.default; if (options.persist) { const value = localStorage.getItem(KEY); if (value !== null) { @@ -134,11 +138,31 @@ export const initLocale = ( setLocale(_locale); }; +/** + * Clear the locale preference previously presisted to localStorage + * + * If you are doing this, you should probably call `setLocale` + * or `i18next.changeLanguage` (depending on your setup) immediately + * before this with `convertToSupportedLocaleOrDefault(getPreferredLocale())` + * so the current locale is set to user's preferred locale. + * + * Note if `persist` is `true` when initializing, + * subsequence `setLocale` calls will still persist the value. + */ +export const clearPersistedLocalePreference = (): void => { + localStorage.removeItem(KEY); +}; + /** Get the current selected locale */ export const getLocale = (): string => { return locale; }; +/** Get the default locale when initialized */ +export const getDefaultLocale = (): string => { + return defaultLocale; +}; + /** * Set the selected locale * @@ -181,7 +205,6 @@ export const setLocale = (newLocale: string): boolean => { * console.log(convertToSupportedLocale("zh-TW")); // "zh" * console.log(convertToSupportedLocale("es")); // undefined * ``` - * */ export const convertToSupportedLocale = ( newLocale: string, @@ -199,6 +222,19 @@ export const convertToSupportedLocale = ( return undefined; }; +/** + * Convert a locale/language to a supported locale/language, + * or return the default locale if not found. + * + * This is a thin wrapper for `convertToSupportedLocale`. + * See that function for more details. + */ +export const convertToSupportedLocaleOrDefault = ( + newLocale: string, +): string => { + return convertToSupportedLocale(newLocale) || defaultLocale; +}; + /** * Add a subscriber to be notified when the locale changes * diff --git a/react/deno.jsonc b/react/deno.jsonc index 3d82f44..7f907c6 100644 --- a/react/deno.jsonc +++ b/react/deno.jsonc @@ -1,6 +1,6 @@ { "name": "@pistonite/pure-react", - "version": "0.0.2", + "version": "0.0.3", "exports": { ".": "./src/index.ts", }, @@ -8,7 +8,7 @@ "exclude": ["package-lock.json", "node_modules/**", "Taskfile.yml"], }, "imports": { - "@pistonite/pure": "jsr:@pistonite/pure@^0.0.10", + "@pistonite/pure": "jsr:@pistonite/pure@^0.0.11", }, "lint": { "rules": { diff --git a/react/package.json b/react/package.json index 85c362a..6c21a52 100644 --- a/react/package.json +++ b/react/package.json @@ -1,11 +1,11 @@ { "peerDependencies": { - "@pistonite/pure": "npm:@jsr/pistonite__pure@^0.0.10", + "@pistonite/pure": "npm:@jsr/pistonite__pure@^0.0.11", "react": "^18" }, "devDependencies": { "@types/react": "^18.3.12", "react": "^18", - "typescript": "^5.5.4" + "typescript": "^5.7.2" } } From e337e1ed48bed071a688ed1418b566260935c7a0 Mon Sep 17 00:00:00 2001 From: Pistonight Date: Mon, 25 Nov 2024 23:23:16 -0800 Subject: [PATCH 2/4] fix dep --- react/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/react/package.json b/react/package.json index 6c21a52..74db1f8 100644 --- a/react/package.json +++ b/react/package.json @@ -1,6 +1,6 @@ { "peerDependencies": { - "@pistonite/pure": "npm:@jsr/pistonite__pure@^0.0.11", + "@pistonite/pure": "npm:@jsr/pistonite__pure@^0.0.10", "react": "^18" }, "devDependencies": { From e6f008435edc605626eb66eba72201d1ee38aebc Mon Sep 17 00:00:00 2001 From: Pistonight Date: Mon, 25 Nov 2024 23:24:21 -0800 Subject: [PATCH 3/4] fix dep again --- react/deno.jsonc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/react/deno.jsonc b/react/deno.jsonc index 7f907c6..0dbd372 100644 --- a/react/deno.jsonc +++ b/react/deno.jsonc @@ -8,7 +8,7 @@ "exclude": ["package-lock.json", "node_modules/**", "Taskfile.yml"], }, "imports": { - "@pistonite/pure": "jsr:@pistonite/pure@^0.0.11", + "@pistonite/pure": "jsr:@pistonite/pure@^0.0.10", }, "lint": { "rules": { From 57426336e81fd22d56fc7d9453246f9aeac7aa78 Mon Sep 17 00:00:00 2001 From: Pistonight Date: Mon, 25 Nov 2024 23:25:43 -0800 Subject: [PATCH 4/4] fixup deps --- react/deno.jsonc | 2 +- react/package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/react/deno.jsonc b/react/deno.jsonc index 0dbd372..7f907c6 100644 --- a/react/deno.jsonc +++ b/react/deno.jsonc @@ -8,7 +8,7 @@ "exclude": ["package-lock.json", "node_modules/**", "Taskfile.yml"], }, "imports": { - "@pistonite/pure": "jsr:@pistonite/pure@^0.0.10", + "@pistonite/pure": "jsr:@pistonite/pure@^0.0.11", }, "lint": { "rules": { diff --git a/react/package.json b/react/package.json index 74db1f8..6c21a52 100644 --- a/react/package.json +++ b/react/package.json @@ -1,6 +1,6 @@ { "peerDependencies": { - "@pistonite/pure": "npm:@jsr/pistonite__pure@^0.0.10", + "@pistonite/pure": "npm:@jsr/pistonite__pure@^0.0.11", "react": "^18" }, "devDependencies": {