File tree Expand file tree Collapse file tree 3 files changed +74
-6
lines changed Expand file tree Collapse file tree 3 files changed +74
-6
lines changed Original file line number Diff line number Diff line change @@ -18,13 +18,41 @@ export const LocalStorageProvider: React.FC<{ children: React.ReactNode }> = ({
1818} ) => {
1919 const [ values , setValues ] = useState < LocalStorageType > ( DEFAULT_LOCAL_STORAGE ) ;
2020
21- // TODO setvalue
22- useEffect ( ( ) => {
21+ // Helper function to sync state with localStorage
22+ const syncWithLocalStorage = ( ) => {
2323 const isJetbrains = getLocalStorage ( "ide" ) === "jetbrains" ;
24- let fontSize = getLocalStorage ( "fontSize" ) ?? ( isJetbrains ? 15 : 14 ) ;
25- setValues ( {
24+ const fontSize = getLocalStorage ( "fontSize" ) ?? ( isJetbrains ? 15 : 14 ) ;
25+
26+ setValues ( ( prev ) => ( {
27+ ...prev ,
2628 fontSize,
27- } ) ;
29+ } ) ) ;
30+ } ;
31+
32+ // Initialize with values from localStorage
33+ useEffect ( ( ) => {
34+ syncWithLocalStorage ( ) ;
35+ } , [ ] ) ;
36+
37+ // Listen for current tab changes using CustomEvent
38+ useEffect ( ( ) => {
39+ const handleLocalStorageChange = ( event : CustomEvent ) => {
40+ if ( event . detail ?. key === "fontSize" ) {
41+ syncWithLocalStorage ( ) ;
42+ }
43+ } ;
44+
45+ window . addEventListener (
46+ "localStorageChange" ,
47+ handleLocalStorageChange as EventListener ,
48+ ) ;
49+
50+ return ( ) => {
51+ window . removeEventListener (
52+ "localStorageChange" ,
53+ handleLocalStorageChange as EventListener ,
54+ ) ;
55+ } ;
2856 } , [ ] ) ;
2957
3058 return (
Original file line number Diff line number Diff line change @@ -79,7 +79,40 @@ export function UserSetting(props: UserSettingProps) {
7979 < input
8080 type = "number"
8181 value = { props . value }
82- onChange = { ( e ) => props . onChange ( Number ( e . target . value ) ) }
82+ onChange = { ( e ) => {
83+ const value = Number ( e . target . value ) ;
84+ // Allow temporary invalid values during input
85+ props . onChange ( value ) ;
86+ } }
87+ onBlur = { ( e ) => {
88+ // Apply min/max constraints when input loses focus
89+ const value = Number ( e . target . value ) ;
90+ const min = props . min ?? 0 ;
91+ const max = props . max ?? 100 ;
92+
93+ if ( value < min ) {
94+ props . onChange ( min ) ;
95+ } else if ( value > max ) {
96+ props . onChange ( max ) ;
97+ }
98+ } }
99+ onKeyDown = { ( e ) => {
100+ // Apply constraints when user presses Enter
101+ if ( e . key === "Enter" ) {
102+ const value = Number ( e . currentTarget . value ) ;
103+ const min = props . min ?? 0 ;
104+ const max = props . max ?? 100 ;
105+
106+ if ( value < min ) {
107+ props . onChange ( min ) ;
108+ } else if ( value > max ) {
109+ props . onChange ( max ) ;
110+ }
111+
112+ // Blur the input to complete the editing
113+ e . currentTarget . blur ( ) ;
114+ }
115+ } }
83116 min = { props . min ?? 0 }
84117 max = { props . max ?? 100 }
85118 disabled = { disabled }
Original file line number Diff line number Diff line change @@ -49,4 +49,11 @@ export function setLocalStorage<T extends keyof LocalStorageTypes>(
4949 value : LocalStorageTypes [ T ] ,
5050) : void {
5151 localStorage . setItem ( key , JSON . stringify ( value ) ) ;
52+
53+ // Dispatch custom event to notify current tab listeners
54+ window . dispatchEvent (
55+ new CustomEvent ( "localStorageChange" , {
56+ detail : { key, value } ,
57+ } ) ,
58+ ) ;
5259}
You can’t perform that action at this time.
0 commit comments