@ui-sdk/web 0.0.0-stage → 0.1.0-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/README.md +10 -2
- package/browser/web/accordion.js +1 -0
- package/browser/web/alert-dialog.js +1 -0
- package/browser/web/autocomplete.js +1 -0
- package/browser/web/avatar.js +1 -0
- package/browser/web/button.js +1 -0
- package/browser/web/calendar.js +1 -0
- package/browser/web/carousel.js +1 -0
- package/browser/web/checkbox.js +1 -0
- package/browser/web/chunks/0eRGibL6.js +1 -0
- package/browser/web/chunks/2bIEUO6_.js +1 -0
- package/browser/web/chunks/5A9w2ULz.js +1 -0
- package/browser/web/chunks/5RSyydjV.js +1 -0
- package/browser/web/chunks/9pWTy2wl.js +1 -0
- package/browser/web/chunks/B0f2j6zz.js +1 -0
- package/browser/web/chunks/B1rux6br.js +1 -0
- package/browser/web/chunks/B3-APaNf.js +1 -0
- package/browser/web/chunks/B7EgSgjQ.js +1 -0
- package/browser/web/chunks/B8QcOzJU.js +1 -0
- package/browser/web/chunks/BFueEkwm.js +3 -0
- package/browser/web/chunks/BJj3oS29.js +1 -0
- package/browser/web/chunks/BNSvTCpr.js +1 -0
- package/browser/web/chunks/BPD0kAZB.js +1 -0
- package/browser/web/chunks/BQeduZqT.js +1 -0
- package/browser/web/chunks/B_CVFyiz.js +1 -0
- package/browser/web/chunks/B_ns1YHO.js +1 -0
- package/browser/web/chunks/BcwkVTsR.js +1 -0
- package/browser/web/chunks/BeaQ6TYM.js +1 -0
- package/browser/web/chunks/BgBB013u.js +1 -0
- package/browser/web/chunks/BgsKhHDm.js +1 -0
- package/browser/web/chunks/BjMcbuhi.js +9 -0
- package/browser/web/chunks/Bjiy_F2N.js +1 -0
- package/browser/web/chunks/BqScCLwv.js +1 -0
- package/browser/web/chunks/BqX3Utqx.js +1 -0
- package/browser/web/chunks/BtVkQf3z.js +1 -0
- package/browser/web/chunks/By30h-7B.js +1 -0
- package/browser/web/chunks/C1zqYpy2.js +1 -0
- package/browser/web/chunks/C436juFf.js +1 -0
- package/browser/web/chunks/C4_bULs1.js +1 -0
- package/browser/web/chunks/C4omqJEz.js +1 -0
- package/browser/web/chunks/C7tt0T3n.js +1 -0
- package/browser/web/chunks/C8w7s5dg.js +1 -0
- package/browser/web/chunks/CElY4erO.js +1 -0
- package/browser/web/chunks/CFODo-m2.js +1 -0
- package/browser/web/chunks/CL4wjam9.js +1 -0
- package/browser/web/chunks/CMg-8UVf.js +1 -0
- package/browser/web/chunks/COWKfakj.js +1 -0
- package/browser/web/chunks/CQBU0qZ4.js +1 -0
- package/browser/web/chunks/CV6uABgc.js +1 -0
- package/browser/web/chunks/C_SWFpWQ.js +1 -0
- package/browser/web/chunks/C_Uanbrl.js +1 -0
- package/browser/web/chunks/Ce_CSanC.js +1 -0
- package/browser/web/chunks/Cg-ZmRhS.js +1 -0
- package/browser/web/chunks/Cgf2wPI_.js +1 -0
- package/browser/web/chunks/Cuep_3Vf.js +1 -0
- package/browser/web/chunks/D02PaHn6.js +1 -0
- package/browser/web/chunks/D5LiaI5R.js +1 -0
- package/browser/web/chunks/D9UgVYvF.js +1 -0
- package/browser/web/chunks/DC08tnkE.js +2 -0
- package/browser/web/chunks/DG8YGSvu.js +1 -0
- package/browser/web/chunks/DIQd0FYD.js +1 -0
- package/browser/web/chunks/DMLqacE8.js +1 -0
- package/browser/web/chunks/DSUI3yI-.js +1 -0
- package/browser/web/chunks/DT3Hb76X.js +1 -0
- package/browser/web/chunks/DY75g3-W.js +1 -0
- package/browser/web/chunks/DZpNQKZ9.js +1 -0
- package/browser/web/chunks/Db-W1wrC.js +1 -0
- package/browser/web/chunks/Dc1xK3Ii.js +1 -0
- package/browser/web/chunks/DdF6awFs.js +1 -0
- package/browser/web/chunks/DdaCLml8.js +1 -0
- package/browser/web/chunks/Dh8MLjQO.js +1 -0
- package/browser/web/chunks/DiuslJRy.js +1 -0
- package/browser/web/chunks/DjRcw3Y0.js +1 -0
- package/browser/web/chunks/Djdv-16J.js +1 -0
- package/browser/web/chunks/DnFXe_lF.js +1 -0
- package/browser/web/chunks/DneGv1oC.js +1 -0
- package/browser/web/chunks/DsVxP-p-.js +1 -0
- package/browser/web/chunks/DwDilCo2.js +1 -0
- package/browser/web/chunks/DwV48AXv.js +1 -0
- package/browser/web/chunks/F37aF4-L.js +1 -0
- package/browser/web/chunks/LyB6XIbN.js +1 -0
- package/browser/web/chunks/NKnbYGjC.js +1 -0
- package/browser/web/chunks/Vk-rVi16.js +1 -0
- package/browser/web/chunks/XsM0J1Ex.js +1 -0
- package/browser/web/chunks/asSN9iEp.js +1 -0
- package/browser/web/chunks/c-AjHfdF.js +1 -0
- package/browser/web/chunks/dR-12TPY.js +1 -0
- package/browser/web/chunks/hR4xM0EC.js +1 -0
- package/browser/web/chunks/hqOaBxAg.js +1 -0
- package/browser/web/chunks/kMKqqeG4.js +1 -0
- package/browser/web/chunks/kXboBcfi.js +1 -0
- package/browser/web/chunks/l4aQIg22.js +1 -0
- package/browser/web/chunks/tJC0JSU4.js +1 -0
- package/browser/web/chunks/wHdS_QYM.js +1 -0
- package/browser/web/chunks/yv8dTvNI.js +1 -0
- package/browser/web/chunks/yw9M3nfi.js +1 -0
- package/browser/web/collapsible.js +1 -0
- package/browser/web/color-picker.js +1 -0
- package/browser/web/combobox.js +2 -0
- package/browser/web/command.js +1 -0
- package/browser/web/core.js +5 -0
- package/browser/web/date-input.js +1 -0
- package/browser/web/date-picker.js +1 -0
- package/browser/web/dialog.js +1 -0
- package/browser/web/drawer.js +1 -0
- package/browser/web/field.js +1 -0
- package/browser/web/file-input.js +1 -0
- package/browser/web/hover-card.js +1 -0
- package/browser/web/image-cropper.js +1 -0
- package/browser/web/input.js +1 -0
- package/browser/web/label.js +1 -0
- package/browser/web/listbox.js +1 -0
- package/browser/web/menu.js +1 -0
- package/browser/web/menubar.js +1 -0
- package/browser/web/meter.js +1 -0
- package/browser/web/navigation-menu.js +1 -0
- package/browser/web/number-input.js +1 -0
- package/browser/web/otp-input.js +2 -0
- package/browser/web/overlays.js +1 -0
- package/browser/web/pagination.js +1 -0
- package/browser/web/password-input.js +1 -0
- package/browser/web/popover.js +1 -0
- package/browser/web/preferences.js +1 -0
- package/browser/web/progress.js +1 -0
- package/browser/web/radio.js +1 -0
- package/browser/web/rating.js +1 -0
- package/browser/web/resizable.js +1 -0
- package/browser/web/scroll-area.js +1 -0
- package/browser/web/segmented-control.js +1 -0
- package/browser/web/select.js +1 -0
- package/browser/web/separator.js +1 -0
- package/browser/web/slider.js +1 -0
- package/browser/web/switch.js +1 -0
- package/browser/web/tabs.js +1 -0
- package/browser/web/textarea.js +1 -0
- package/browser/web/time-input.js +1 -0
- package/browser/web/timeline.js +1 -0
- package/browser/web/toast.js +1 -0
- package/browser/web/toggle.js +1 -0
- package/browser/web/token-input.js +8 -0
- package/browser/web/toolbar.js +1 -0
- package/browser/web/tooltip.js +1 -0
- package/browser/web/tree.js +1 -0
- package/browser/web/trigger-menu.js +4 -0
- package/browser/web/waterfall.js +1 -0
- package/browser/web.css +3378 -0
- package/browser/web.js +3 -0
- package/custom-elements.json +32315 -0
- package/dist/accordion.d.ts +2 -0
- package/dist/accordion.js +57 -0
- package/dist/action.d.ts +16 -0
- package/dist/action.js +39 -0
- package/dist/agent.d.ts +2 -0
- package/dist/agent.js +7 -0
- package/dist/autocomplete.d.ts +2 -0
- package/dist/autocomplete.js +120 -0
- package/dist/autoload.d.ts +18 -0
- package/dist/autoload.js +63 -0
- package/dist/avatar.d.ts +2 -0
- package/dist/avatar.js +27 -0
- package/dist/browser-core.d.ts +2 -0
- package/dist/browser-core.js +8 -0
- package/dist/button.d.ts +2 -0
- package/dist/button.js +35 -0
- package/dist/calendar.d.ts +23 -0
- package/dist/calendar.js +429 -0
- package/dist/carousel.d.ts +2 -0
- package/dist/carousel.js +54 -0
- package/dist/checkbox.d.ts +4 -0
- package/dist/checkbox.js +75 -0
- package/dist/collapsible.d.ts +2 -0
- package/dist/collapsible.js +28 -0
- package/dist/color-picker.d.ts +2 -0
- package/dist/color-picker.js +64 -0
- package/dist/combobox.d.ts +2 -0
- package/dist/combobox.js +334 -0
- package/dist/command.d.ts +2 -0
- package/dist/command.js +122 -0
- package/dist/date-input.d.ts +3 -0
- package/dist/date-input.js +87 -0
- package/dist/date-picker.d.ts +2 -0
- package/dist/date-picker.js +174 -0
- package/dist/default-text.d.ts +3 -0
- package/dist/default-text.js +24 -0
- package/dist/define-element.d.ts +404 -0
- package/dist/define-element.js +1489 -0
- package/dist/dialog.d.ts +3 -0
- package/dist/dialog.js +94 -0
- package/dist/drawer.d.ts +2 -0
- package/dist/drawer.js +96 -0
- package/dist/entries/accordion.d.ts +3 -0
- package/dist/entries/accordion.js +7 -0
- package/dist/entries/alert-dialog.d.ts +3 -0
- package/dist/entries/alert-dialog.js +13 -0
- package/dist/entries/autocomplete.d.ts +3 -0
- package/dist/entries/autocomplete.js +7 -0
- package/dist/entries/avatar.d.ts +2 -0
- package/dist/entries/avatar.js +3 -0
- package/dist/entries/button.d.ts +2 -0
- package/dist/entries/button.js +3 -0
- package/dist/entries/calendar.d.ts +3 -0
- package/dist/entries/calendar.js +7 -0
- package/dist/entries/carousel.d.ts +3 -0
- package/dist/entries/carousel.js +7 -0
- package/dist/entries/checkbox.d.ts +3 -0
- package/dist/entries/checkbox.js +15 -0
- package/dist/entries/collapsible.d.ts +3 -0
- package/dist/entries/collapsible.js +7 -0
- package/dist/entries/color-picker.d.ts +3 -0
- package/dist/entries/color-picker.js +7 -0
- package/dist/entries/combobox.d.ts +3 -0
- package/dist/entries/combobox.js +7 -0
- package/dist/entries/command.d.ts +3 -0
- package/dist/entries/command.js +7 -0
- package/dist/entries/date-input.d.ts +3 -0
- package/dist/entries/date-input.js +7 -0
- package/dist/entries/date-picker.d.ts +3 -0
- package/dist/entries/date-picker.js +7 -0
- package/dist/entries/dialog.d.ts +3 -0
- package/dist/entries/dialog.js +7 -0
- package/dist/entries/drawer.d.ts +3 -0
- package/dist/entries/drawer.js +8 -0
- package/dist/entries/field.d.ts +2 -0
- package/dist/entries/field.js +3 -0
- package/dist/entries/file-input.d.ts +3 -0
- package/dist/entries/file-input.js +13 -0
- package/dist/entries/hover-card.d.ts +3 -0
- package/dist/entries/hover-card.js +7 -0
- package/dist/entries/image-cropper.d.ts +3 -0
- package/dist/entries/image-cropper.js +7 -0
- package/dist/entries/input.d.ts +3 -0
- package/dist/entries/input.js +8 -0
- package/dist/entries/label.d.ts +2 -0
- package/dist/entries/label.js +3 -0
- package/dist/entries/listbox.d.ts +3 -0
- package/dist/entries/listbox.js +7 -0
- package/dist/entries/menu.d.ts +3 -0
- package/dist/entries/menu.js +7 -0
- package/dist/entries/menubar.d.ts +3 -0
- package/dist/entries/menubar.js +7 -0
- package/dist/entries/meter.d.ts +2 -0
- package/dist/entries/meter.js +3 -0
- package/dist/entries/navigation-menu.d.ts +3 -0
- package/dist/entries/navigation-menu.js +7 -0
- package/dist/entries/number-input.d.ts +3 -0
- package/dist/entries/number-input.js +7 -0
- package/dist/entries/otp-input.d.ts +3 -0
- package/dist/entries/otp-input.js +7 -0
- package/dist/entries/overlays.d.ts +2 -0
- package/dist/entries/overlays.js +3 -0
- package/dist/entries/pagination.d.ts +3 -0
- package/dist/entries/pagination.js +11 -0
- package/dist/entries/password-input.d.ts +3 -0
- package/dist/entries/password-input.js +7 -0
- package/dist/entries/popover.d.ts +3 -0
- package/dist/entries/popover.js +7 -0
- package/dist/entries/preferences.d.ts +3 -0
- package/dist/entries/preferences.js +13 -0
- package/dist/entries/progress.d.ts +2 -0
- package/dist/entries/progress.js +3 -0
- package/dist/entries/radio.d.ts +3 -0
- package/dist/entries/radio.js +7 -0
- package/dist/entries/rating.d.ts +3 -0
- package/dist/entries/rating.js +8 -0
- package/dist/entries/resizable.d.ts +3 -0
- package/dist/entries/resizable.js +11 -0
- package/dist/entries/scroll-area.d.ts +3 -0
- package/dist/entries/scroll-area.js +7 -0
- package/dist/entries/segmented-control.d.ts +3 -0
- package/dist/entries/segmented-control.js +7 -0
- package/dist/entries/select.d.ts +3 -0
- package/dist/entries/select.js +7 -0
- package/dist/entries/separator.d.ts +2 -0
- package/dist/entries/separator.js +3 -0
- package/dist/entries/slider.d.ts +3 -0
- package/dist/entries/slider.js +7 -0
- package/dist/entries/switch.d.ts +3 -0
- package/dist/entries/switch.js +7 -0
- package/dist/entries/tabs.d.ts +3 -0
- package/dist/entries/tabs.js +7 -0
- package/dist/entries/textarea.d.ts +3 -0
- package/dist/entries/textarea.js +7 -0
- package/dist/entries/time-input.d.ts +3 -0
- package/dist/entries/time-input.js +7 -0
- package/dist/entries/timeline.d.ts +3 -0
- package/dist/entries/timeline.js +7 -0
- package/dist/entries/toast.d.ts +2 -0
- package/dist/entries/toast.js +3 -0
- package/dist/entries/toggle.d.ts +3 -0
- package/dist/entries/toggle.js +12 -0
- package/dist/entries/token-input.d.ts +3 -0
- package/dist/entries/token-input.js +17 -0
- package/dist/entries/toolbar.d.ts +3 -0
- package/dist/entries/toolbar.js +7 -0
- package/dist/entries/tooltip.d.ts +3 -0
- package/dist/entries/tooltip.js +7 -0
- package/dist/entries/tree.d.ts +3 -0
- package/dist/entries/tree.js +17 -0
- package/dist/entries/trigger-menu.d.ts +3 -0
- package/dist/entries/trigger-menu.js +11 -0
- package/dist/entries/waterfall.d.ts +3 -0
- package/dist/entries/waterfall.js +11 -0
- package/dist/events.d.ts +1658 -0
- package/dist/events.js +0 -0
- package/dist/field.d.ts +2 -0
- package/dist/field.js +101 -0
- package/dist/file-input.d.ts +2 -0
- package/dist/file-input.js +140 -0
- package/dist/forward-attributes.d.ts +2 -0
- package/dist/forward-attributes.js +22 -0
- package/dist/history.d.ts +2 -0
- package/dist/history.js +11 -0
- package/dist/hover-card.d.ts +2 -0
- package/dist/hover-card.js +31 -0
- package/dist/image-cropper.d.ts +2 -0
- package/dist/image-cropper.js +66 -0
- package/dist/index.d.ts +66 -0
- package/dist/index.js +269 -0
- package/dist/input.d.ts +3 -0
- package/dist/input.js +69 -0
- package/dist/label.d.ts +2 -0
- package/dist/label.js +11 -0
- package/dist/list.d.ts +40 -0
- package/dist/list.js +192 -0
- package/dist/listbox.d.ts +2 -0
- package/dist/listbox.js +84 -0
- package/dist/menu.d.ts +4 -0
- package/dist/menu.js +295 -0
- package/dist/menubar.d.ts +4 -0
- package/dist/menubar.js +30 -0
- package/dist/meter.d.ts +2 -0
- package/dist/meter.js +22 -0
- package/dist/navigation-menu.d.ts +2 -0
- package/dist/navigation-menu.js +114 -0
- package/dist/number-input.d.ts +2 -0
- package/dist/number-input.js +73 -0
- package/dist/observe.d.ts +2 -0
- package/dist/observe.js +7 -0
- package/dist/otp-input.d.ts +2 -0
- package/dist/otp-input.js +72 -0
- package/dist/overlays.d.ts +13 -0
- package/dist/overlays.js +156 -0
- package/dist/pagination.d.ts +2 -0
- package/dist/pagination.js +127 -0
- package/dist/password-input.d.ts +2 -0
- package/dist/password-input.js +57 -0
- package/dist/popover.d.ts +2 -0
- package/dist/popover.js +64 -0
- package/dist/popup.d.ts +40 -0
- package/dist/popup.js +112 -0
- package/dist/preferences.d.ts +2 -0
- package/dist/preferences.js +56 -0
- package/dist/progress.d.ts +2 -0
- package/dist/progress.js +22 -0
- package/dist/radio.d.ts +2 -0
- package/dist/radio.js +33 -0
- package/dist/rating.d.ts +2 -0
- package/dist/rating.js +64 -0
- package/dist/resizable.d.ts +2 -0
- package/dist/resizable.js +42 -0
- package/dist/scroll-area.d.ts +2 -0
- package/dist/scroll-area.js +45 -0
- package/dist/segmented-control.d.ts +2 -0
- package/dist/segmented-control.js +28 -0
- package/dist/select.d.ts +2 -0
- package/dist/select.js +239 -0
- package/dist/separator.d.ts +2 -0
- package/dist/separator.js +11 -0
- package/dist/slider.d.ts +2 -0
- package/dist/slider.js +77 -0
- package/dist/tabs.d.ts +2 -0
- package/dist/tabs.js +37 -0
- package/dist/timeline.d.ts +2 -0
- package/dist/timeline.js +27 -0
- package/dist/toast.d.ts +20 -0
- package/dist/toast.js +177 -0
- package/dist/toggle.d.ts +3 -0
- package/dist/toggle.js +64 -0
- package/dist/token-input.d.ts +2 -0
- package/dist/token-input.js +103 -0
- package/dist/toolbar.d.ts +2 -0
- package/dist/toolbar.js +36 -0
- package/dist/tooltip.d.ts +2 -0
- package/dist/tooltip.js +35 -0
- package/dist/tree.d.ts +2 -0
- package/dist/tree.js +147 -0
- package/dist/trigger-menu.d.ts +2 -0
- package/dist/trigger-menu.js +133 -0
- package/dist/waterfall.d.ts +2 -0
- package/dist/waterfall.js +140 -0
- package/docs/README.md +89 -0
- package/docs/agents.md +1523 -0
- package/docs/components/accordion.md +1401 -0
- package/docs/components/alert-dialog.md +788 -0
- package/docs/components/autocomplete.md +1692 -0
- package/docs/components/avatar.md +261 -0
- package/docs/components/button.md +441 -0
- package/docs/components/calendar.md +1536 -0
- package/docs/components/carousel.md +1055 -0
- package/docs/components/checkbox.md +1045 -0
- package/docs/components/collapsible.md +535 -0
- package/docs/components/color-picker.md +651 -0
- package/docs/components/combobox.md +3274 -0
- package/docs/components/command.md +1431 -0
- package/docs/components/date-input.md +563 -0
- package/docs/components/date-picker.md +1136 -0
- package/docs/components/dialog.md +1360 -0
- package/docs/components/drawer.md +1201 -0
- package/docs/components/field.md +519 -0
- package/docs/components/file-input.md +617 -0
- package/docs/components/hover-card.md +486 -0
- package/docs/components/image-cropper.md +489 -0
- package/docs/components/input.md +327 -0
- package/docs/components/label.md +208 -0
- package/docs/components/listbox.md +1015 -0
- package/docs/components/menu.md +1212 -0
- package/docs/components/menubar.md +460 -0
- package/docs/components/meter.md +298 -0
- package/docs/components/navigation-menu.md +1573 -0
- package/docs/components/number-input.md +1087 -0
- package/docs/components/otp-input.md +558 -0
- package/docs/components/pagination.md +639 -0
- package/docs/components/password-input.md +649 -0
- package/docs/components/popover.md +1433 -0
- package/docs/components/preferences.md +243 -0
- package/docs/components/progress.md +406 -0
- package/docs/components/radio.md +855 -0
- package/docs/components/rating.md +431 -0
- package/docs/components/resizable.md +866 -0
- package/docs/components/scroll-area.md +898 -0
- package/docs/components/segmented-control.md +421 -0
- package/docs/components/select.md +1957 -0
- package/docs/components/separator.md +238 -0
- package/docs/components/slider.md +1018 -0
- package/docs/components/switch.md +583 -0
- package/docs/components/tabs.md +948 -0
- package/docs/components/textarea.md +331 -0
- package/docs/components/time-input.md +235 -0
- package/docs/components/timeline.md +622 -0
- package/docs/components/toast.md +1223 -0
- package/docs/components/toggle.md +522 -0
- package/docs/components/token-input.md +854 -0
- package/docs/components/toolbar.md +815 -0
- package/docs/components/tooltip.md +860 -0
- package/docs/components/tree.md +1243 -0
- package/docs/components/trigger-menu.md +1110 -0
- package/docs/components/waterfall.md +709 -0
- package/docs/controllers.md +66 -0
- package/docs/core.md +8027 -0
- package/docs/custom-parts.md +91 -0
- package/docs/first-component.md +88 -0
- package/docs/forms.md +125 -0
- package/docs/installation.md +141 -0
- package/docs/introduction.md +32 -0
- package/docs/lists-and-menus.md +166 -0
- package/docs/llms.txt +1566 -0
- package/docs/parts-and-composition.md +57 -0
- package/docs/popups.md +92 -0
- package/docs/rendering.md +71 -0
- package/docs/server-rendering.md +118 -0
- package/docs/state-and-events.md +134 -0
- package/docs/styling-and-animation.md +104 -0
- package/docs/utilities/async-options.md +332 -0
- package/docs/utilities/shortcuts.md +184 -0
- package/html-custom-data.json +14792 -0
- package/package.json +302 -3
|
@@ -0,0 +1,583 @@
|
|
|
1
|
+
# Switch
|
|
2
|
+
|
|
3
|
+
Turn a setting on or off.
|
|
4
|
+
|
|
5
|
+
Use a switch for a setting that takes effect as soon as it changes, like Airplane mode or a notification preference. Keep its label the same in both states: the label says what the setting is, and the switch shows whether it's on.
|
|
6
|
+
|
|
7
|
+
```ts title="basic.ts"
|
|
8
|
+
import "@ui-sdk/web/switch"
|
|
9
|
+
|
|
10
|
+
function mount(root: HTMLElement) {
|
|
11
|
+
root.innerHTML = `
|
|
12
|
+
<label data-slot="label">
|
|
13
|
+
Airplane mode
|
|
14
|
+
<ui-switch>
|
|
15
|
+
<ui-switch-thumb></ui-switch-thumb>
|
|
16
|
+
</ui-switch>
|
|
17
|
+
</label>
|
|
18
|
+
`
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export { mount }
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
Click Airplane mode, or focus the switch and press Space, to turn it on and off.
|
|
25
|
+
|
|
26
|
+
## Installation
|
|
27
|
+
|
|
28
|
+
```bash
|
|
29
|
+
npm install @ui-sdk/web @ui-sdk/core
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
Importing the component defines its elements. Import the field too if you use it. On the server the import does nothing.
|
|
33
|
+
|
|
34
|
+
```ts
|
|
35
|
+
import "@ui-sdk/web/switch"
|
|
36
|
+
import "@ui-sdk/web/field"
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## Composition
|
|
40
|
+
|
|
41
|
+
Use the following composition to build a `ui-switch`:
|
|
42
|
+
|
|
43
|
+
```text
|
|
44
|
+
ui-switch
|
|
45
|
+
└── ui-switch-thumb
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
Wrap the switch and its text in a label. The thumb stays mounted and gets the switch's state, so CSS can slide it from one end to the other.
|
|
49
|
+
|
|
50
|
+
## Usage
|
|
51
|
+
|
|
52
|
+
Import the parts and put them together:
|
|
53
|
+
|
|
54
|
+
```html
|
|
55
|
+
<script type="module">
|
|
56
|
+
import "@ui-sdk/web/switch"
|
|
57
|
+
</script>
|
|
58
|
+
|
|
59
|
+
<label>
|
|
60
|
+
Airplane mode
|
|
61
|
+
<ui-switch>
|
|
62
|
+
<ui-switch-thumb></ui-switch-thumb>
|
|
63
|
+
</ui-switch>
|
|
64
|
+
</label>
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## Description
|
|
68
|
+
|
|
69
|
+
Use `ui-field-label` and `ui-field-description` inside a `ui-field` when a setting needs more explanation. The field connects both to the switch.
|
|
70
|
+
|
|
71
|
+
```ts title="description.ts"
|
|
72
|
+
import "@ui-sdk/web/switch"
|
|
73
|
+
import "@ui-sdk/web/field"
|
|
74
|
+
|
|
75
|
+
function mount(root: HTMLElement) {
|
|
76
|
+
root.innerHTML = `
|
|
77
|
+
<ui-field>
|
|
78
|
+
<div data-slot="stack">
|
|
79
|
+
<ui-field-label>Weekly digest</ui-field-label>
|
|
80
|
+
<ui-field-description>A summary of your week, every Monday.</ui-field-description>
|
|
81
|
+
</div>
|
|
82
|
+
<ui-switch>
|
|
83
|
+
<ui-switch-thumb></ui-switch-thumb>
|
|
84
|
+
</ui-switch>
|
|
85
|
+
</ui-field>
|
|
86
|
+
`
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export { mount }
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
Click Weekly digest. The field connects the label and description to the switch.
|
|
93
|
+
|
|
94
|
+
## Choice Card
|
|
95
|
+
|
|
96
|
+
A setting can fill a whole card. Here the label's `::after` covers the card, so a click anywhere on it toggles the switch, and `:has([data-checked])` marks the cards that are on.
|
|
97
|
+
|
|
98
|
+
```ts title="choice-card.ts"
|
|
99
|
+
import "@ui-sdk/web/switch"
|
|
100
|
+
import "@ui-sdk/web/field"
|
|
101
|
+
|
|
102
|
+
const settings = [
|
|
103
|
+
{
|
|
104
|
+
label: "Public profile",
|
|
105
|
+
description: "Anyone can see your name and photo.",
|
|
106
|
+
defaultChecked: false,
|
|
107
|
+
},
|
|
108
|
+
{
|
|
109
|
+
label: "Show activity status",
|
|
110
|
+
description: "Teammates can see when you're online.",
|
|
111
|
+
defaultChecked: true,
|
|
112
|
+
},
|
|
113
|
+
]
|
|
114
|
+
|
|
115
|
+
function mount(root: HTMLElement) {
|
|
116
|
+
root.innerHTML = `
|
|
117
|
+
<div data-slot="stack">
|
|
118
|
+
${settings
|
|
119
|
+
.map(
|
|
120
|
+
(setting) => `
|
|
121
|
+
<ui-field data-variant="card">
|
|
122
|
+
<div data-slot="stack">
|
|
123
|
+
<ui-field-label>${setting.label}</ui-field-label>
|
|
124
|
+
<ui-field-description>${setting.description}</ui-field-description>
|
|
125
|
+
</div>
|
|
126
|
+
<ui-switch${setting.defaultChecked ? " default-checked" : ""}>
|
|
127
|
+
<ui-switch-thumb></ui-switch-thumb>
|
|
128
|
+
</ui-switch>
|
|
129
|
+
</ui-field>
|
|
130
|
+
`
|
|
131
|
+
)
|
|
132
|
+
.join("")}
|
|
133
|
+
</div>
|
|
134
|
+
`
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
export { mount }
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
Click anywhere on the Public profile card to turn it on. Each card's switch keeps its own value.
|
|
141
|
+
|
|
142
|
+
## Disabled
|
|
143
|
+
|
|
144
|
+
`disabled` prevents focus and changes, and leaves the switch out of form submissions. A disabled `fieldset` does the same.
|
|
145
|
+
|
|
146
|
+
```ts title="disabled.ts"
|
|
147
|
+
import "@ui-sdk/web/switch"
|
|
148
|
+
|
|
149
|
+
function mount(root: HTMLElement) {
|
|
150
|
+
root.innerHTML = `
|
|
151
|
+
<div data-slot="stack">
|
|
152
|
+
<label data-slot="label">
|
|
153
|
+
Product updates
|
|
154
|
+
<ui-switch>
|
|
155
|
+
<ui-switch-thumb></ui-switch-thumb>
|
|
156
|
+
</ui-switch>
|
|
157
|
+
</label>
|
|
158
|
+
<label data-slot="label">
|
|
159
|
+
Security alerts
|
|
160
|
+
<ui-switch default-checked disabled>
|
|
161
|
+
<ui-switch-thumb></ui-switch-thumb>
|
|
162
|
+
</ui-switch>
|
|
163
|
+
</label>
|
|
164
|
+
</div>
|
|
165
|
+
`
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
export { mount }
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
Security alerts are always on, so that switch is disabled. Clicking it leaves it on, and Tab skips it.
|
|
172
|
+
|
|
173
|
+
## Read-Only
|
|
174
|
+
|
|
175
|
+
`read-only` keeps the switch focusable and included in form submissions, while preventing the user from changing it. Use it for a setting managed somewhere else.
|
|
176
|
+
|
|
177
|
+
```ts title="read-only.ts"
|
|
178
|
+
import "@ui-sdk/web/switch"
|
|
179
|
+
import "@ui-sdk/web/field"
|
|
180
|
+
|
|
181
|
+
function mount(root: HTMLElement) {
|
|
182
|
+
root.innerHTML = `
|
|
183
|
+
<ui-field>
|
|
184
|
+
<div data-slot="stack">
|
|
185
|
+
<ui-field-label>Encrypt backups</ui-field-label>
|
|
186
|
+
<ui-field-description>Your organization manages this setting.</ui-field-description>
|
|
187
|
+
</div>
|
|
188
|
+
<ui-switch name="encrypt" default-checked read-only>
|
|
189
|
+
<ui-switch-thumb></ui-switch-thumb>
|
|
190
|
+
</ui-switch>
|
|
191
|
+
</ui-field>
|
|
192
|
+
`
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
export { mount }
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
Tab to Encrypt backups. Space and clicks leave it on.
|
|
199
|
+
|
|
200
|
+
## Controlled
|
|
201
|
+
|
|
202
|
+
Listen for `checkedchange` to follow the value, and set the element's `checked` property to change it. The element's [`api`](#controller) changes it from anywhere, like the Toggle button below. See [State and Events](../state-and-events.md) for the shared change callback.
|
|
203
|
+
|
|
204
|
+
```ts title="controlled.ts"
|
|
205
|
+
import "@ui-sdk/web/switch"
|
|
206
|
+
import { log } from "./log"
|
|
207
|
+
|
|
208
|
+
function mount(root: HTMLElement) {
|
|
209
|
+
root.innerHTML = `
|
|
210
|
+
<div data-slot="stack">
|
|
211
|
+
<div data-slot="controls">
|
|
212
|
+
<button type="button">Toggle</button>
|
|
213
|
+
</div>
|
|
214
|
+
<label data-slot="label">
|
|
215
|
+
Wi-Fi
|
|
216
|
+
<ui-switch default-checked>
|
|
217
|
+
<ui-switch-thumb></ui-switch-thumb>
|
|
218
|
+
</ui-switch>
|
|
219
|
+
</label>
|
|
220
|
+
</div>
|
|
221
|
+
`
|
|
222
|
+
const wifi = root.querySelector("ui-switch")!
|
|
223
|
+
root
|
|
224
|
+
.querySelector("button")!
|
|
225
|
+
.addEventListener("click", () => wifi.api.toggle())
|
|
226
|
+
wifi.addEventListener("checkedchange", (event) => {
|
|
227
|
+
const { value, reason } = event.detail
|
|
228
|
+
log(root, `${value ? "On" : "Off"} (${reason})`)
|
|
229
|
+
})
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
export { mount }
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
Turn Wi-Fi off, or press Toggle. The console logs each change and its reason.
|
|
236
|
+
|
|
237
|
+
## Form
|
|
238
|
+
|
|
239
|
+
Set `name` to include the switch in a native form. A switch that's on submits its `value`, which defaults to `"on"`; a switch that's off submits nothing. Reset returns it to `default-checked`. Set `form` to a form's id to join it from elsewhere on the page.
|
|
240
|
+
|
|
241
|
+
```ts title="form.ts"
|
|
242
|
+
import "@ui-sdk/web/switch"
|
|
243
|
+
import "@ui-sdk/web/field"
|
|
244
|
+
import { log } from "./log"
|
|
245
|
+
|
|
246
|
+
function mount(root: HTMLElement) {
|
|
247
|
+
root.innerHTML = `
|
|
248
|
+
<form data-slot="stack">
|
|
249
|
+
<ui-field-set>
|
|
250
|
+
<fieldset>
|
|
251
|
+
<legend>Email notifications</legend>
|
|
252
|
+
<label data-slot="label">
|
|
253
|
+
Product updates
|
|
254
|
+
<ui-switch name="updates">
|
|
255
|
+
<ui-switch-thumb></ui-switch-thumb>
|
|
256
|
+
</ui-switch>
|
|
257
|
+
</label>
|
|
258
|
+
<label data-slot="label">
|
|
259
|
+
Security alerts
|
|
260
|
+
<ui-switch name="security" default-checked>
|
|
261
|
+
<ui-switch-thumb></ui-switch-thumb>
|
|
262
|
+
</ui-switch>
|
|
263
|
+
</label>
|
|
264
|
+
<label data-slot="label">
|
|
265
|
+
Weekly digest
|
|
266
|
+
<ui-switch name="digest" value="weekly" default-checked>
|
|
267
|
+
<ui-switch-thumb></ui-switch-thumb>
|
|
268
|
+
</ui-switch>
|
|
269
|
+
</label>
|
|
270
|
+
</fieldset>
|
|
271
|
+
</ui-field-set>
|
|
272
|
+
<div data-slot="row">
|
|
273
|
+
<button type="submit" data-variant="solid">Save</button>
|
|
274
|
+
<button type="reset">Reset</button>
|
|
275
|
+
</div>
|
|
276
|
+
</form>
|
|
277
|
+
`
|
|
278
|
+
const form = root.querySelector("form")!
|
|
279
|
+
form.addEventListener("submit", (event) => {
|
|
280
|
+
event.preventDefault()
|
|
281
|
+
const entries = [...new FormData(form)].map(
|
|
282
|
+
([name, value]) => `${name}=${value}`
|
|
283
|
+
)
|
|
284
|
+
log(
|
|
285
|
+
root,
|
|
286
|
+
entries.length ? `Submitted ${entries.join("&")}` : "Submitted nothing"
|
|
287
|
+
)
|
|
288
|
+
})
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
export { mount }
|
|
292
|
+
```
|
|
293
|
+
|
|
294
|
+
Turn on Product updates and press Save. The console logs the submitted values, and Reset restores the defaults.
|
|
295
|
+
|
|
296
|
+
## Validation
|
|
297
|
+
|
|
298
|
+
`required` blocks submission until the switch is on. In a [Field](./field.md), `ui-field-error` shows the message under the switch and connects it to the switch. [Forms and Validation](../forms.md) covers the shared field behavior.
|
|
299
|
+
|
|
300
|
+
```ts title="invalid.ts"
|
|
301
|
+
import "@ui-sdk/web/switch"
|
|
302
|
+
import "@ui-sdk/web/field"
|
|
303
|
+
import { log } from "./log"
|
|
304
|
+
|
|
305
|
+
function mount(root: HTMLElement) {
|
|
306
|
+
root.innerHTML = `
|
|
307
|
+
<form data-slot="stack">
|
|
308
|
+
<ui-field>
|
|
309
|
+
<div data-slot="row">
|
|
310
|
+
<div data-slot="stack">
|
|
311
|
+
<ui-field-label>Two-factor authentication</ui-field-label>
|
|
312
|
+
<ui-field-description>Required for admins of this workspace.</ui-field-description>
|
|
313
|
+
</div>
|
|
314
|
+
<ui-switch name="two-factor" required>
|
|
315
|
+
<ui-switch-thumb></ui-switch-thumb>
|
|
316
|
+
</ui-switch>
|
|
317
|
+
</div>
|
|
318
|
+
<ui-field-error match="valueMissing">Turn this on to continue.</ui-field-error>
|
|
319
|
+
</ui-field>
|
|
320
|
+
<div data-slot="row">
|
|
321
|
+
<button type="submit" data-variant="solid">Continue</button>
|
|
322
|
+
</div>
|
|
323
|
+
</form>
|
|
324
|
+
`
|
|
325
|
+
const form = root.querySelector("form")!
|
|
326
|
+
form.addEventListener("submit", (event) => {
|
|
327
|
+
event.preventDefault()
|
|
328
|
+
log(root, `Submitted two-factor=${new FormData(form).get("two-factor")}`)
|
|
329
|
+
})
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
export { mount }
|
|
333
|
+
```
|
|
334
|
+
|
|
335
|
+
Press Continue with the switch off to see the error. Turn on Two-factor authentication and continue; the console logs the submitted value.
|
|
336
|
+
|
|
337
|
+
## Saving on Toggle
|
|
338
|
+
|
|
339
|
+
To save a change before it shows, call `event.preventDefault()` in `checkedchange` to keep the current value while the save runs. When it finishes, call `api.setChecked(next, { reason: "imperative" })`, and let that reason through in the listener so the saved value commits.
|
|
340
|
+
|
|
341
|
+
The example ignores presses while a save is running, and shows a spinner in the thumb. `aria-busy` tells assistive technology the switch is busy; the listener is what blocks another change.
|
|
342
|
+
|
|
343
|
+
```ts title="async.ts"
|
|
344
|
+
import "@ui-sdk/web/switch"
|
|
345
|
+
import { createElement, LoaderCircle } from "lucide"
|
|
346
|
+
import { log } from "./log"
|
|
347
|
+
|
|
348
|
+
const save = (_checked: boolean) =>
|
|
349
|
+
new Promise((resolve) => setTimeout(resolve, 800))
|
|
350
|
+
|
|
351
|
+
function mount(root: HTMLElement) {
|
|
352
|
+
root.innerHTML = `
|
|
353
|
+
<label data-slot="label">
|
|
354
|
+
Automatic backups
|
|
355
|
+
<ui-switch aria-busy="false">
|
|
356
|
+
<ui-switch-thumb></ui-switch-thumb>
|
|
357
|
+
</ui-switch>
|
|
358
|
+
</label>
|
|
359
|
+
`
|
|
360
|
+
const backups = root.querySelector("ui-switch")!
|
|
361
|
+
const thumb = root.querySelector("ui-switch-thumb")!
|
|
362
|
+
let saving = false
|
|
363
|
+
|
|
364
|
+
backups.addEventListener("checkedchange", (event) => {
|
|
365
|
+
const { value, reason } = event.detail
|
|
366
|
+
if (reason === "imperative") {
|
|
367
|
+
return
|
|
368
|
+
}
|
|
369
|
+
event.preventDefault()
|
|
370
|
+
if (saving) {
|
|
371
|
+
return
|
|
372
|
+
}
|
|
373
|
+
saving = true
|
|
374
|
+
backups.setAttribute("aria-busy", "true")
|
|
375
|
+
thumb.replaceChildren(
|
|
376
|
+
createElement(LoaderCircle, {
|
|
377
|
+
"data-slot": "spinner",
|
|
378
|
+
"aria-hidden": "true",
|
|
379
|
+
})
|
|
380
|
+
)
|
|
381
|
+
log(root, "Saving…")
|
|
382
|
+
save(value).then(() => {
|
|
383
|
+
saving = false
|
|
384
|
+
backups.api.setChecked(value, { reason: "imperative" })
|
|
385
|
+
backups.setAttribute("aria-busy", "false")
|
|
386
|
+
thumb.replaceChildren()
|
|
387
|
+
log(root, `Saved: ${value ? "on" : "off"}`)
|
|
388
|
+
})
|
|
389
|
+
})
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
export { mount }
|
|
393
|
+
```
|
|
394
|
+
|
|
395
|
+
Turn on Automatic backups. The thumb shows a spinner while it saves, then the switch turns on.
|
|
396
|
+
|
|
397
|
+
## Custom Rendering
|
|
398
|
+
|
|
399
|
+
Elements have no render functions. `ui-switch` is the focusable control and `ui-switch-thumb` holds whatever you put inside it. Here a `checkedchange` listener puts a sun or a moon in the thumb, and the stylesheet can read `data-checked` on either part. See [Parts and Composition](../parts-and-composition.md) for the rendering contract.
|
|
400
|
+
|
|
401
|
+
```ts title="render.ts"
|
|
402
|
+
import "@ui-sdk/web/switch"
|
|
403
|
+
import { createElement, Moon, Sun } from "lucide"
|
|
404
|
+
|
|
405
|
+
function mount(root: HTMLElement) {
|
|
406
|
+
root.innerHTML = `
|
|
407
|
+
<label data-slot="label">
|
|
408
|
+
Dark mode
|
|
409
|
+
<ui-switch>
|
|
410
|
+
<ui-switch-thumb></ui-switch-thumb>
|
|
411
|
+
</ui-switch>
|
|
412
|
+
</label>
|
|
413
|
+
`
|
|
414
|
+
const darkMode = root.querySelector("ui-switch")!
|
|
415
|
+
const thumb = root.querySelector("ui-switch-thumb")!
|
|
416
|
+
const render = (checked: boolean) =>
|
|
417
|
+
thumb.replaceChildren(
|
|
418
|
+
createElement(checked ? Moon : Sun, { "aria-hidden": "true" })
|
|
419
|
+
)
|
|
420
|
+
render(darkMode.checked)
|
|
421
|
+
darkMode.addEventListener("checkedchange", (event) =>
|
|
422
|
+
render(event.detail.value)
|
|
423
|
+
)
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
export { mount }
|
|
427
|
+
```
|
|
428
|
+
|
|
429
|
+
Turn on Dark mode. The thumb renders a sun or a moon from its state.
|
|
430
|
+
|
|
431
|
+
## Styling
|
|
432
|
+
|
|
433
|
+
Style the switch and its thumb with `data-checked` and `data-unchecked`, and use `data-disabled`, `data-readonly` and `data-required` for the rest. In an invalid field, the switch also gets `data-invalid` and `aria-invalid`; the thumb doesn't. Slide the thumb with `translate` on `data-checked`, and reverse it under `dir="rtl"`.
|
|
434
|
+
|
|
435
|
+
Each part has a `data-slot`, such as `switch`, and its state attributes are listed under [Data Attributes](#data-attributes).
|
|
436
|
+
|
|
437
|
+
## Data Attributes
|
|
438
|
+
|
|
439
|
+
### `<ui-switch>`
|
|
440
|
+
`[data-slot="switch"]`
|
|
441
|
+
|
|
442
|
+
- `data-checked`: Present while on.
|
|
443
|
+
- `data-disabled`: Present when disabled, by its own prop or a `<fieldset>`.
|
|
444
|
+
- `data-readonly`: Present when read-only.
|
|
445
|
+
- `data-required`: Present when required.
|
|
446
|
+
- `data-unchecked`: Present while off.
|
|
447
|
+
- `data-invalid`: Present while a `Field` around it shows as invalid. The thumb doesn't get it.
|
|
448
|
+
|
|
449
|
+
### `<ui-switch-thumb>`
|
|
450
|
+
`[data-slot="switch-thumb"]`
|
|
451
|
+
|
|
452
|
+
- `data-checked`: Present while on.
|
|
453
|
+
- `data-disabled`: Present when disabled, by its own prop or a `<fieldset>`.
|
|
454
|
+
- `data-readonly`: Present when read-only.
|
|
455
|
+
- `data-required`: Present when required.
|
|
456
|
+
- `data-unchecked`: Present while off.
|
|
457
|
+
|
|
458
|
+
## API Reference
|
|
459
|
+
|
|
460
|
+
Props are attributes in kebab case, like `default-checked` and `read-only`: `"false"` is false. `checked` is a live property and attribute. Callbacks are DOM events.
|
|
461
|
+
|
|
462
|
+
### Switch
|
|
463
|
+
|
|
464
|
+
- `checked?` (`boolean`): Whether it's checked. Pass it to control the checkbox.
|
|
465
|
+
- `defaultChecked?` (`boolean`): Whether it's checked at first, when uncontrolled. A form reset returns to it.
|
|
466
|
+
- `checkedchange event`: Runs when a user or a form reset wants to change it. `details.reason` says why, and `details.cancel()` stops it.
|
|
467
|
+
- `disabled?` (`boolean`): Disables it like a native control: it can't be focused or changed, and isn't submitted.
|
|
468
|
+
- `readOnly?` (`boolean`): Keeps it focusable and submitted, but stops changes.
|
|
469
|
+
- `required?` (`boolean`): Blocks form submission until it's checked, with the browser's own message.
|
|
470
|
+
- `name?` (`string`): The name it submits under. Without one, it submits nothing.
|
|
471
|
+
- `value?` (`string`): What it submits while checked.
|
|
472
|
+
- `form?` (`string`): The id of a form to belong to, when it isn't inside that form.
|
|
473
|
+
- `controller?` (`{ toggle: (details?: Details) => boolean; getChecked: () => boolean; setChecked: (checked: boolean, details?: Details) => boolean; focus: () => void; }`): Toggles it from anywhere. Create one with `createSwitchController()`.
|
|
474
|
+
- `debug?` (`boolean`): Logs every state change with its reason to the console, in development builds.
|
|
475
|
+
|
|
476
|
+
### State
|
|
477
|
+
|
|
478
|
+
The same state shows as data attributes on each element.
|
|
479
|
+
|
|
480
|
+
- `checked` (`boolean`): Whether it's checked. False while indeterminate.
|
|
481
|
+
- `disabled` (`boolean`): Whether it's disabled, by itself or by a `<fieldset>`.
|
|
482
|
+
- `readOnly` (`boolean`): Whether it's read-only: it stays focusable and submitted, but presses don't change it.
|
|
483
|
+
- `required` (`boolean`): Whether a form won't submit until it's checked. Indeterminate doesn't count.
|
|
484
|
+
|
|
485
|
+
### Reasons
|
|
486
|
+
|
|
487
|
+
`event.detail.reason` says why the switch turned on or off.
|
|
488
|
+
|
|
489
|
+
- `press` (`MouseEvent | KeyboardEvent`): A single-element control was pressed: a checkbox, a switch or a toggle.
|
|
490
|
+
- `reset` (`none`): A form reset.
|
|
491
|
+
- `restore` (`none`): The browser restored state, like after going back, or a saved value was read back from storage.
|
|
492
|
+
- `imperative` (`none`): A controller or `api` call, or a property set on a web component.
|
|
493
|
+
|
|
494
|
+
### Controller
|
|
495
|
+
|
|
496
|
+
The element's `api` property has the same methods. The `checked` property reads and sets the state.
|
|
497
|
+
|
|
498
|
+
Every write method takes an optional `{ reason, event }`, fires the change event like a press, and returns `false` when the change is canceled.
|
|
499
|
+
|
|
500
|
+
- `toggle(details?)` (`(details?: CheckboxChangeOptions | undefined) => boolean`): Turns it on, or off.
|
|
501
|
+
- `getChecked()` (`() => boolean`): Whether it's on.
|
|
502
|
+
- `setChecked(checked, details?)` (`(checked: boolean, details?: CheckboxChangeOptions | undefined) => boolean`): Turns it on or off, as a user would.
|
|
503
|
+
- `focus()` (`() => void`): Moves focus to it.
|
|
504
|
+
|
|
505
|
+
### Events
|
|
506
|
+
|
|
507
|
+
Events fire on their own element and don't bubble, with the new state in `event.detail.value` and the `reason`, `event` and `source` beside it. Calling `preventDefault()` cancels the change.
|
|
508
|
+
|
|
509
|
+
| Member | On | Description |
|
|
510
|
+
| ----------------------------- | ----------- | ------------------------------------------------------------------------- |
|
|
511
|
+
| `checked` property, attribute | `ui-switch` | Whether the switch is on. The attribute sets where it starts, and is live |
|
|
512
|
+
| `api` property | `ui-switch` | The [controller](#controller) |
|
|
513
|
+
| `checkedchange` event | `ui-switch` | `detail: { value: checked, reason, event, source }` |
|
|
514
|
+
|
|
515
|
+
## Accessibility
|
|
516
|
+
|
|
517
|
+
The switch follows the [WAI-ARIA switch pattern](https://www.w3.org/WAI/ARIA/apg/patterns/switch/). `ui-switch` takes the `switch` role itself and takes part in forms through `ElementInternals`, with no hidden input.
|
|
518
|
+
|
|
519
|
+
### Keyboard
|
|
520
|
+
|
|
521
|
+
| Key | Does |
|
|
522
|
+
| ----- | -------------------------------------- |
|
|
523
|
+
| Tab | Moves to the next switch |
|
|
524
|
+
| Space | Turns it on or off |
|
|
525
|
+
| Enter | Submits its form, and never toggles it |
|
|
526
|
+
|
|
527
|
+
### Tested Behavior
|
|
528
|
+
|
|
529
|
+
Every line below is an automated test.
|
|
530
|
+
|
|
531
|
+
- Starts in its default state.
|
|
532
|
+
- Clicking toggles it.
|
|
533
|
+
- Clicking its label toggles it.
|
|
534
|
+
- Space toggles it.
|
|
535
|
+
- Enter submits the form and never toggles it.
|
|
536
|
+
- The thumb follows the state.
|
|
537
|
+
|
|
538
|
+
**The first input on a page**
|
|
539
|
+
|
|
540
|
+
- The first Space reports keyboard.
|
|
541
|
+
|
|
542
|
+
**On a touch screen**
|
|
543
|
+
|
|
544
|
+
- The first tap reports touch.
|
|
545
|
+
|
|
546
|
+
Runs in React, Vue and web components.
|
|
547
|
+
|
|
548
|
+
In forms:
|
|
549
|
+
|
|
550
|
+
- The form submits every checked control's name and value.
|
|
551
|
+
- Unchecked controls submit nothing.
|
|
552
|
+
- A checkbox outside the form joins it with the form attribute.
|
|
553
|
+
- Required controls block submission.
|
|
554
|
+
- A form reset returns every control to its default.
|
|
555
|
+
- A reset returns to the default value, not to nothing.
|
|
556
|
+
- A disabled fieldset disables the controls inside it.
|
|
557
|
+
- A read-only control still submits.
|
|
558
|
+
- A user's change fires native input and change events on the form.
|
|
559
|
+
|
|
560
|
+
Runs in React, Vue and web components.
|
|
561
|
+
|
|
562
|
+
Beyond the pattern's baseline:
|
|
563
|
+
|
|
564
|
+
**Enhancements**
|
|
565
|
+
|
|
566
|
+
- The first invalid control takes focus in document order.
|
|
567
|
+
- A later required field without an error part doesn't take focus from the first.
|
|
568
|
+
|
|
569
|
+
**Form-associated custom elements**
|
|
570
|
+
|
|
571
|
+
- Take part in the form with no hidden input. Web components only.
|
|
572
|
+
- Report validity like native controls. Web components only.
|
|
573
|
+
- Have live checked and value properties. Web components only.
|
|
574
|
+
- Are named by their labels. Web components only.
|
|
575
|
+
|
|
576
|
+
Runs in React, Vue and web components.
|
|
577
|
+
|
|
578
|
+
Screen readers hear the same thing in each tested framework, recorded by a virtual screen reader:
|
|
579
|
+
|
|
580
|
+
- Announcements while checking boxes with a keyboard.
|
|
581
|
+
- Announcements while choosing a radio with a keyboard.
|
|
582
|
+
|
|
583
|
+
Runs in React, Vue and web components.
|