Implement ProgCode identity and real shared reading UI kit
Build and deploy / deploy (push) Successful in 2m39s
Build and deploy / deploy (push) Successful in 2m39s
This commit is contained in:
1 parent
99c848189e
commit
12562baeb5
2040 files changed
+184656
-181
No files matched your search
+20
@@ -0,0 +1,20 @@
|
||||
"use client";
|
||||
import {useEffect,useId,useState} from 'react';
|
||||
import {THEME_STORAGE_KEY,THEME_CHANGE_EVENT,THEME_OPTIONS,validTheme} from '../../../shared/config';
|
||||
function readPreference(){try{const value=localStorage.getItem(THEME_STORAGE_KEY);return validTheme(value)?value:'system';}catch{return 'system';}}
|
||||
export function ThemeControl(){
|
||||
const id=useId();const [theme,setTheme]=useState('system');
|
||||
useEffect(()=>{
|
||||
const sync=event=>{const value=validTheme(event?.detail)?event.detail:readPreference();setTheme(value);document.documentElement.dataset.theme=value;};
|
||||
const onStorage=event=>{if(event.key===THEME_STORAGE_KEY||event.key===null)sync();};
|
||||
sync();window.addEventListener(THEME_CHANGE_EVENT,sync);window.addEventListener('storage',onStorage);
|
||||
return()=>{window.removeEventListener(THEME_CHANGE_EVENT,sync);window.removeEventListener('storage',onStorage);};
|
||||
},[]);
|
||||
function change(event){
|
||||
const value=event.target.value;if(!validTheme(value))return;
|
||||
setTheme(value);document.documentElement.dataset.theme=value;
|
||||
try{localStorage.setItem(THEME_STORAGE_KEY,value);}catch{/* Preference remains usable for this page without storage. */}
|
||||
window.dispatchEvent(new CustomEvent(THEME_CHANGE_EVENT,{detail:value}));
|
||||
}
|
||||
return <div className="theme-control"><label htmlFor={id}>Тема</label><select id={id} value={theme} onChange={change}>{THEME_OPTIONS.map(option=><option value={option.value} key={option.value}>{option.label}</option>)}</select></div>;
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
import {THEME_STORAGE_KEY} from '../../../shared/config';
|
||||
// Constant script contains only the storage key and allowlisted preference values.
|
||||
export function ThemeInit(){const code=`try{var t=localStorage.getItem(${JSON.stringify(THEME_STORAGE_KEY)});document.documentElement.dataset.theme=(t==='light'||t==='dark'||t==='system')?t:'system';}catch(e){document.documentElement.dataset.theme='system';}`;return <script id="progcode-theme-init" dangerouslySetInnerHTML={{__html:code}}/>;}
|
||||
Reference in new issue
Block a user