Skip to content

Commit

Permalink
fix bug
Browse files Browse the repository at this point in the history
  • Loading branch information
JerryWu1234 committed Oct 24, 2024
1 parent 80a69b0 commit 924a6a7
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export default component$(() => {
const checked = useSignal(true)
useStyles$(styles);
return (
<Switch.Root class="switch" bind:checked={checked}>
<Switch.Root class="switch" bind:checked={checked}>
<Switch.Label>test</Switch.Label>
<Switch.Input />
</Switch.Root>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@
box-shadow: inset 0 0 0 2px hsl(0 0% 100% / 10%);
}
}

&:focus {
outline: 2px solid hsl(var(--primary));
outline-offset: 2px;
}
}
}

Expand Down
25 changes: 7 additions & 18 deletions packages/kit-headless/src/components/switch/switch-input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,15 @@ export const SwitchInput = component$<PropsOf<'input'>>(() => {
const context = useContext(SwitchContext)
const id = useId()
if(context.defaultChecked && context.bindChecked && !context.bindChecked.value){
context.bindChecked.value = true
context.bindChecked.value = !context.bindChecked.value
}

if(context.autoFocus && !context.switchRef?.value){
context.switchRef?.value?.focus()
}


const handleKeyDownSync$ = sync$((e: KeyboardEvent) => {
if (e.key === ' ') {
e.preventDefault();
}
});

const handleKeyDown$ = $((e: KeyboardEvent) => {
if (e.key === ' ') {
context.bindChecked.value = !context.bindChecked.value;
}
});

const handleClick$ = $((e: MouseEvent | KeyboardEvent) => {
context.switchRef?.value?.focus()
context.bindChecked.value = !context.bindChecked.value;
if(context.onChange$){
context.onChange$(context.bindChecked.value, e)
Expand All @@ -36,7 +24,9 @@ export const SwitchInput = component$<PropsOf<'input'>>(() => {
}

});

const handleClickSync$ = sync$((e: MouseEvent) => {
e.preventDefault();
});
return (
<input
data-checked={context.bindChecked?.value ? '' : undefined}
Expand All @@ -47,10 +37,9 @@ export const SwitchInput = component$<PropsOf<'input'>>(() => {
aria-checked={context.bindChecked ? 'true' : 'false'}
type="checkbox"
role="switch"
onClick$={handleClick$}
onClick$={[handleClickSync$, handleClick$]}
checked={context.bindChecked?.value}
onChange$={handleClick$}
onKeyDown$={[handleKeyDownSync$, handleKeyDown$]}
onChange$={[handleClickSync$,handleClick$]}
onKeyPress$={handleClick$}
/>
);
Expand Down

0 comments on commit 924a6a7

Please sign in to comment.