Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add a new switch component #960

Open
wants to merge 66 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
66 commits
Select commit Hold shift + click to select a range
dbe9fc4
feat: add a new switch component
JerryWu1234 Sep 13, 2024
40c8fba
Merge branch 'main' into add_component_switch
JerryWu1234 Sep 19, 2024
a219d8f
init debug
JerryWu1234 Sep 19, 2024
df815cf
Merge branch 'main' into add_component_switch
JerryWu1234 Sep 23, 2024
7a5f627
switch is developing
JerryWu1234 Sep 24, 2024
5850f72
headless Switch
JerryWu1234 Sep 24, 2024
61e5eb4
add css style
JerryWu1234 Sep 25, 2024
5d18929
Merge branch 'main' into add_component_switch
JerryWu1234 Oct 4, 2024
e167210
Merge branch 'main' into add_component_switch
JerryWu1234 Oct 7, 2024
fa4d78d
Merge branch 'main' into add_component_switch
JerryWu1234 Oct 15, 2024
75b226e
switch function
JerryWu1234 Oct 15, 2024
6da7f8f
Merge branch 'add_component_switch' of https://github.com/JerryWu1234…
JerryWu1234 Oct 15, 2024
6c112bb
add more properties
JerryWu1234 Oct 17, 2024
80a69b0
Merge branch 'main' into add_component_switch
JerryWu1234 Oct 24, 2024
924a6a7
fix bug
JerryWu1234 Oct 24, 2024
d30ee27
fix conflict
JerryWu1234 Oct 31, 2024
4aeca25
add test
JerryWu1234 Nov 4, 2024
9cc504c
add switch component in headless
JerryWu1234 Nov 6, 2024
db01981
Merge branch 'main' into add_component_switch
JerryWu1234 Nov 14, 2024
2839aa6
fix switch
JerryWu1234 Nov 14, 2024
c62ddda
fix switch
JerryWu1234 Nov 15, 2024
521c3b0
developing styled
JerryWu1234 Nov 15, 2024
2f54039
developing styled
JerryWu1234 Nov 15, 2024
a132016
css
JerryWu1234 Nov 18, 2024
b51919a
css
JerryWu1234 Nov 18, 2024
e78c0c4
add css
JerryWu1234 Nov 18, 2024
1d7562a
Merge branch 'main' into add_component_switch
JerryWu1234 Nov 20, 2024
8aae55d
test
JerryWu1234 Nov 21, 2024
32cf8bc
Merge branch 'add_component_switch' of https://github.com/JerryWu1234…
JerryWu1234 Nov 21, 2024
eb3f351
test
JerryWu1234 Nov 21, 2024
2324120
Merge branch 'main' into add_component_switch
JerryWu1234 Nov 26, 2024
482d258
fix css
JerryWu1234 Nov 27, 2024
89bddb5
Merge branch 'main' into add_component_switch
JerryWu1234 Nov 29, 2024
4ae9b4e
fix
JerryWu1234 Nov 29, 2024
28bbe12
Merge branch 'add_component_switch' of https://github.com/JerryWu1234…
JerryWu1234 Nov 29, 2024
d58521e
fix
JerryWu1234 Dec 3, 2024
d2eb94a
fix
JerryWu1234 Dec 3, 2024
db37e73
switch
JerryWu1234 Dec 3, 2024
b176836
switch
JerryWu1234 Dec 3, 2024
a10ddbb
Merge branch 'main' into add_component_switch
JerryWu1234 Dec 17, 2024
c02a82e
Merge branch 'main' into add_component_switch
JerryWu1234 Dec 31, 2024
4220133
Merge branch 'main' into add_component_switch
JerryWu1234 Jan 2, 2025
d499152
Merge branch 'main' into add_component_switch
JerryWu1234 Jan 5, 2025
0fa6cfe
fix
JerryWu1234 Jan 5, 2025
ff96a9a
Merge branch 'main' into add_component_switch
JerryWu1234 Jan 5, 2025
258ff6e
swich
JerryWu1234 Jan 5, 2025
e7e5a94
Merge branch 'add_component_switch' of https://github.com/JerryWu1234…
JerryWu1234 Jan 5, 2025
e6969b1
fix
JerryWu1234 Jan 5, 2025
ae47c83
Merge branch 'main' into add_component_switch
JerryWu1234 Jan 31, 2025
296de86
fix Clicking outside the thumb
JerryWu1234 Feb 1, 2025
0a7e6d0
Merge branch 'main' into add_component_switch
JerryWu1234 Feb 13, 2025
c8b9525
fix default value
JerryWu1234 Feb 16, 2025
4fb3eee
add more test and fix ts error
JerryWu1234 Feb 17, 2025
e6835c4
fix bug
JerryWu1234 Feb 27, 2025
eda1332
add more test, enhance demo
JerryWu1234 Mar 2, 2025
799de9a
fix
JerryWu1234 Mar 14, 2025
ce19f13
css
JerryWu1234 Mar 14, 2025
e3d5dd8
fix
JerryWu1234 Mar 14, 2025
c511e7d
fix by comment
JerryWu1234 Mar 21, 2025
efbbbc9
Merge branch 'add_component_switch' of https://github.com/JerryWu1234…
JerryWu1234 Mar 21, 2025
7cee977
add useBoundSignal
JerryWu1234 Mar 24, 2025
979d0c0
fix name
JerryWu1234 Mar 24, 2025
8c08e38
fix filename
JerryWu1234 Mar 25, 2025
e701143
Merge branch 'main' into add_component_switch
JerryWu1234 Mar 25, 2025
adac7ed
feat: optimize it according to comment
JerryWu1234 Mar 26, 2025
52b9c9c
Merge branch 'add_component_switch' of https://github.com/JerryWu1234…
JerryWu1234 Mar 26, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
headless Switch
  • Loading branch information
JerryWu1234 committed Sep 24, 2024
commit 5850f72027c7f45ac1a05c3b0cb6767b9b3f6bdd
29 changes: 6 additions & 23 deletions apps/website/src/routes/docs/headless/switch/examples/hero.tsx
Original file line number Diff line number Diff line change
@@ -1,31 +1,14 @@
import { component$, useStyles$ } from '@builder.io/qwik';
import { Select } from '@qwik-ui/headless';
import { component$ } from '@builder.io/qwik';
import { Switch } from '@qwik-ui/headless';

export default component$(() => {
useStyles$(styles);
const users = ['Tim', 'Ryan', 'Jim', 'Jessie', 'Abby'];

return (
<Select.Root class="select">
<Select.Label>Logged in users</Select.Label>
<Select.Trigger class="select-trigger">
<Select.DisplayValue placeholder="Select an option" />
</Select.Trigger>
<Select.Popover class="select-popover">
{users.map((user) => (
<Select.Item class="select-item" key={user}>
<Select.ItemLabel>{user}</Select.ItemLabel>
<Select.ItemIndicator>
<LuCheck />
</Select.ItemIndicator>
</Select.Item>
))}
</Select.Popover>
</Select.Root>
<Switch.Root>
<Switch.Label>sdsds</Switch.Label>
<Switch.Input/>
</Switch.Root>
);
});

// internal
import styles from '../snippets/select.css?inline';
import { LuCheck } from '@qwikest/icons/lucide';

4 changes: 3 additions & 1 deletion packages/kit-headless/src/components/switch/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export {}
export { SwitchRoot as Root } from './switch-root'
export { SwitchInput as Input } from './switch-input'
export { SwitchLable as Label } from './switch-lable'
12 changes: 6 additions & 6 deletions packages/kit-headless/src/components/switch/switch-context.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { createContextId, type Signal } from '@builder.io/qwik';
import { createContextId, QRL, type Signal } from '@builder.io/qwik';

export interface SwitchState {
'bind:checked': Signal<boolean>;
defaultChecked: boolean;
disabled: boolean;
label: string;
'bind:checked'?: Signal<boolean>;
defaultChecked?: boolean;
disabled?: boolean;
onChange$?: QRL<() => void>
}
//
export type SwitchContextState = Omit<SwitchState, 'bind:checked'> & { bindChecked: Signal<boolean> }
export type SwitchContextState = Omit<SwitchState, 'bind:checked'> & { bindChecked?: Signal<boolean> }

export const SwitchContext = createContextId<SwitchContextState>('SwitchContext');
14 changes: 14 additions & 0 deletions packages/kit-headless/src/components/switch/switch-input.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

import { component$, PropsOf, useContext } from '@builder.io/qwik';
import { SwitchContext } from './switch-context';
export const SwitchInput = component$<PropsOf<'input'>>(() => {
const context = useContext(SwitchContext)
return (
<input
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using the input here as the interaction point defeats the purpose of the headless component. We are trying to avoid the limitations of the native input element.

You can have a visually hidden input with thise role called Switch.Input, or Switch.HiddenInput but the purpose there would be for native form support.

I think you want a button, as it naturally calls the onClick$ function on the enter and spacebar keys.

type="checkbox"
role="switch"
onChange$={context.onChange$}
/>
);
},
);
10 changes: 10 additions & 0 deletions packages/kit-headless/src/components/switch/switch-lable.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

import { component$, PropsOf, Slot } from '@builder.io/qwik';
export const SwitchLable = component$<PropsOf<'lable'>>(() => {
return (
<label>
<Slot></Slot>
</label>
);
},
);
5 changes: 3 additions & 2 deletions packages/kit-headless/src/components/switch/switch-root.tsx
Original file line number Diff line number Diff line change
@@ -9,12 +9,13 @@ export type SwitchProps = PropsOf<'div'> & SwitchState;



export const SwitchRoot = component$(({defaultChecked, disabled, label, ...rest}: SwitchProps) => {
export const SwitchRoot = component$(({defaultChecked, disabled, label, onChange$, ...rest}: SwitchProps) => {
const context: SwitchContextState = {
defaultChecked,
disabled,
label,
bindChecked: rest['bind:checked']
bindChecked: rest['bind:checked'],
onChange$: onChange$
}

useContextProvider(SwitchContext, context)