@vitral/themes 0.1.0
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 +165 -0
- package/LICENSE.GPL +674 -0
- package/README.md +29 -0
- package/dist/css/avalonia.css +2279 -0
- package/dist/css/ink.css +2266 -0
- package/dist/css/prism.css +2266 -0
- package/dist/css/simple.css +2277 -0
- package/dist/engine/color.d.ts +16 -0
- package/dist/engine/color.js +69 -0
- package/dist/engine/color.js.map +1 -0
- package/dist/engine/compile.d.ts +41 -0
- package/dist/engine/compile.js +102 -0
- package/dist/engine/compile.js.map +1 -0
- package/dist/engine/manager.d.ts +39 -0
- package/dist/engine/manager.js +120 -0
- package/dist/engine/manager.js.map +1 -0
- package/dist/engine/palette.d.ts +12 -0
- package/dist/engine/palette.js +39 -0
- package/dist/engine/palette.js.map +1 -0
- package/dist/engine/preset.d.ts +16 -0
- package/dist/engine/preset.js +35 -0
- package/dist/engine/preset.js.map +1 -0
- package/dist/engine/scheme.d.ts +40 -0
- package/dist/engine/scheme.js +56 -0
- package/dist/engine/scheme.js.map +1 -0
- package/dist/engine/types.d.ts +35 -0
- package/dist/engine/types.js +10 -0
- package/dist/engine/types.js.map +1 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.js +15 -0
- package/dist/presets/astra.d.ts +1 -0
- package/dist/presets/astra.js +169 -0
- package/dist/presets/astra.js.map +1 -0
- package/dist/presets/avalonia.d.ts +1 -0
- package/dist/presets/avalonia.js +279 -0
- package/dist/presets/avalonia.js.map +1 -0
- package/dist/presets/base/components/accordion.d.ts +31 -0
- package/dist/presets/base/components/accordion.js +33 -0
- package/dist/presets/base/components/accordion.js.map +1 -0
- package/dist/presets/base/components/aspectratio.d.ts +6 -0
- package/dist/presets/base/components/aspectratio.js +6 -0
- package/dist/presets/base/components/aspectratio.js.map +1 -0
- package/dist/presets/base/components/autocomplete.d.ts +15 -0
- package/dist/presets/base/components/autocomplete.js +19 -0
- package/dist/presets/base/components/autocomplete.js.map +1 -0
- package/dist/presets/base/components/avatar.d.ts +26 -0
- package/dist/presets/base/components/avatar.js +28 -0
- package/dist/presets/base/components/avatar.js.map +1 -0
- package/dist/presets/base/components/avatargroup.d.ts +8 -0
- package/dist/presets/base/components/avatargroup.js +10 -0
- package/dist/presets/base/components/avatargroup.js.map +1 -0
- package/dist/presets/base/components/badge.d.ts +27 -0
- package/dist/presets/base/components/badge.js +29 -0
- package/dist/presets/base/components/badge.js.map +1 -0
- package/dist/presets/base/components/blockui.d.ts +9 -0
- package/dist/presets/base/components/blockui.js +11 -0
- package/dist/presets/base/components/blockui.js.map +1 -0
- package/dist/presets/base/components/breadcrumb.d.ts +27 -0
- package/dist/presets/base/components/breadcrumb.js +29 -0
- package/dist/presets/base/components/breadcrumb.js.map +1 -0
- package/dist/presets/base/components/button.d.ts +28 -0
- package/dist/presets/base/components/button.js +30 -0
- package/dist/presets/base/components/button.js.map +1 -0
- package/dist/presets/base/components/buttongroup.d.ts +8 -0
- package/dist/presets/base/components/buttongroup.js +10 -0
- package/dist/presets/base/components/buttongroup.js.map +1 -0
- package/dist/presets/base/components/card.d.ts +26 -0
- package/dist/presets/base/components/card.js +26 -0
- package/dist/presets/base/components/card.js.map +1 -0
- package/dist/presets/base/components/carousel.d.ts +23 -0
- package/dist/presets/base/components/carousel.js +27 -0
- package/dist/presets/base/components/carousel.js.map +1 -0
- package/dist/presets/base/components/cascadeselect.d.ts +18 -0
- package/dist/presets/base/components/cascadeselect.js +22 -0
- package/dist/presets/base/components/cascadeselect.js.map +1 -0
- package/dist/presets/base/components/chart.d.ts +145 -0
- package/dist/presets/base/components/chart.js +145 -0
- package/dist/presets/base/components/chart.js.map +1 -0
- package/dist/presets/base/components/checkbox.d.ts +32 -0
- package/dist/presets/base/components/checkbox.js +34 -0
- package/dist/presets/base/components/checkbox.js.map +1 -0
- package/dist/presets/base/components/chip.d.ts +23 -0
- package/dist/presets/base/components/chip.js +23 -0
- package/dist/presets/base/components/chip.js.map +1 -0
- package/dist/presets/base/components/colorpicker.d.ts +31 -0
- package/dist/presets/base/components/colorpicker.js +35 -0
- package/dist/presets/base/components/colorpicker.js.map +1 -0
- package/dist/presets/base/components/command.d.ts +42 -0
- package/dist/presets/base/components/command.js +44 -0
- package/dist/presets/base/components/command.js.map +1 -0
- package/dist/presets/base/components/confirmdialog.d.ts +10 -0
- package/dist/presets/base/components/confirmdialog.js +12 -0
- package/dist/presets/base/components/confirmdialog.js.map +1 -0
- package/dist/presets/base/components/confirmpopup.d.ts +23 -0
- package/dist/presets/base/components/confirmpopup.js +27 -0
- package/dist/presets/base/components/confirmpopup.js.map +1 -0
- package/dist/presets/base/components/contextmenu.d.ts +34 -0
- package/dist/presets/base/components/contextmenu.js +32 -0
- package/dist/presets/base/components/contextmenu.js.map +1 -0
- package/dist/presets/base/components/datatable.d.ts +93 -0
- package/dist/presets/base/components/datatable.js +83 -0
- package/dist/presets/base/components/datatable.js.map +1 -0
- package/dist/presets/base/components/dataview.d.ts +26 -0
- package/dist/presets/base/components/dataview.js +30 -0
- package/dist/presets/base/components/dataview.js.map +1 -0
- package/dist/presets/base/components/datepicker.d.ts +43 -0
- package/dist/presets/base/components/datepicker.js +45 -0
- package/dist/presets/base/components/datepicker.js.map +1 -0
- package/dist/presets/base/components/dialog.d.ts +33 -0
- package/dist/presets/base/components/dialog.js +37 -0
- package/dist/presets/base/components/dialog.js.map +1 -0
- package/dist/presets/base/components/divider.d.ts +21 -0
- package/dist/presets/base/components/divider.js +25 -0
- package/dist/presets/base/components/divider.js.map +1 -0
- package/dist/presets/base/components/dock.d.ts +22 -0
- package/dist/presets/base/components/dock.js +26 -0
- package/dist/presets/base/components/dock.js.map +1 -0
- package/dist/presets/base/components/dockpanel.d.ts +6 -0
- package/dist/presets/base/components/dockpanel.js +6 -0
- package/dist/presets/base/components/dockpanel.js.map +1 -0
- package/dist/presets/base/components/drawer.d.ts +32 -0
- package/dist/presets/base/components/drawer.js +36 -0
- package/dist/presets/base/components/drawer.js.map +1 -0
- package/dist/presets/base/components/dynamicdialog.d.ts +6 -0
- package/dist/presets/base/components/dynamicdialog.js +6 -0
- package/dist/presets/base/components/dynamicdialog.js.map +1 -0
- package/dist/presets/base/components/editor.d.ts +159 -0
- package/dist/presets/base/components/editor.js +169 -0
- package/dist/presets/base/components/editor.js.map +1 -0
- package/dist/presets/base/components/fieldset.d.ts +25 -0
- package/dist/presets/base/components/fieldset.js +27 -0
- package/dist/presets/base/components/fieldset.js.map +1 -0
- package/dist/presets/base/components/fileupload.d.ts +33 -0
- package/dist/presets/base/components/fileupload.js +37 -0
- package/dist/presets/base/components/fileupload.js.map +1 -0
- package/dist/presets/base/components/floatlabel.d.ts +12 -0
- package/dist/presets/base/components/floatlabel.js +14 -0
- package/dist/presets/base/components/floatlabel.js.map +1 -0
- package/dist/presets/base/components/form.d.ts +46 -0
- package/dist/presets/base/components/form.js +44 -0
- package/dist/presets/base/components/form.js.map +1 -0
- package/dist/presets/base/components/galleria.d.ts +33 -0
- package/dist/presets/base/components/galleria.js +35 -0
- package/dist/presets/base/components/galleria.js.map +1 -0
- package/dist/presets/base/components/grid.d.ts +12 -0
- package/dist/presets/base/components/grid.js +16 -0
- package/dist/presets/base/components/grid.js.map +1 -0
- package/dist/presets/base/components/hovercard.d.ts +12 -0
- package/dist/presets/base/components/hovercard.js +14 -0
- package/dist/presets/base/components/hovercard.js.map +1 -0
- package/dist/presets/base/components/iconfield.d.ts +11 -0
- package/dist/presets/base/components/iconfield.js +13 -0
- package/dist/presets/base/components/iconfield.js.map +1 -0
- package/dist/presets/base/components/iftalabel.d.ts +12 -0
- package/dist/presets/base/components/iftalabel.js +14 -0
- package/dist/presets/base/components/iftalabel.js.map +1 -0
- package/dist/presets/base/components/image.d.ts +25 -0
- package/dist/presets/base/components/image.js +25 -0
- package/dist/presets/base/components/image.js.map +1 -0
- package/dist/presets/base/components/index.d.ts +2713 -0
- package/dist/presets/base/components/index.js +213 -0
- package/dist/presets/base/components/index.js.map +1 -0
- package/dist/presets/base/components/inplace.d.ts +11 -0
- package/dist/presets/base/components/inplace.js +13 -0
- package/dist/presets/base/components/inplace.js.map +1 -0
- package/dist/presets/base/components/inputgroup.d.ts +13 -0
- package/dist/presets/base/components/inputgroup.js +15 -0
- package/dist/presets/base/components/inputgroup.js.map +1 -0
- package/dist/presets/base/components/inputgroupaddon.d.ts +10 -0
- package/dist/presets/base/components/inputgroupaddon.js +12 -0
- package/dist/presets/base/components/inputgroupaddon.js.map +1 -0
- package/dist/presets/base/components/inputmask.d.ts +6 -0
- package/dist/presets/base/components/inputmask.js +6 -0
- package/dist/presets/base/components/inputmask.js.map +1 -0
- package/dist/presets/base/components/inputnumber.d.ts +23 -0
- package/dist/presets/base/components/inputnumber.js +25 -0
- package/dist/presets/base/components/inputnumber.js.map +1 -0
- package/dist/presets/base/components/inputotp.d.ts +17 -0
- package/dist/presets/base/components/inputotp.js +15 -0
- package/dist/presets/base/components/inputotp.js.map +1 -0
- package/dist/presets/base/components/inputtext.d.ts +8 -0
- package/dist/presets/base/components/inputtext.js +10 -0
- package/dist/presets/base/components/inputtext.js.map +1 -0
- package/dist/presets/base/components/knob.d.ts +17 -0
- package/dist/presets/base/components/knob.js +15 -0
- package/dist/presets/base/components/knob.js.map +1 -0
- package/dist/presets/base/components/label.d.ts +20 -0
- package/dist/presets/base/components/label.js +18 -0
- package/dist/presets/base/components/label.js.map +1 -0
- package/dist/presets/base/components/listbox.d.ts +22 -0
- package/dist/presets/base/components/listbox.js +22 -0
- package/dist/presets/base/components/listbox.js.map +1 -0
- package/dist/presets/base/components/megamenu.d.ts +33 -0
- package/dist/presets/base/components/megamenu.js +35 -0
- package/dist/presets/base/components/megamenu.js.map +1 -0
- package/dist/presets/base/components/menu.d.ts +40 -0
- package/dist/presets/base/components/menu.js +42 -0
- package/dist/presets/base/components/menu.js.map +1 -0
- package/dist/presets/base/components/menubar.d.ts +43 -0
- package/dist/presets/base/components/menubar.js +41 -0
- package/dist/presets/base/components/menubar.js.map +1 -0
- package/dist/presets/base/components/message.d.ts +25 -0
- package/dist/presets/base/components/message.js +23 -0
- package/dist/presets/base/components/message.js.map +1 -0
- package/dist/presets/base/components/metergroup.d.ts +17 -0
- package/dist/presets/base/components/metergroup.js +21 -0
- package/dist/presets/base/components/metergroup.js.map +1 -0
- package/dist/presets/base/components/multiselect.d.ts +17 -0
- package/dist/presets/base/components/multiselect.js +19 -0
- package/dist/presets/base/components/multiselect.js.map +1 -0
- package/dist/presets/base/components/orderlist.d.ts +26 -0
- package/dist/presets/base/components/orderlist.js +24 -0
- package/dist/presets/base/components/orderlist.js.map +1 -0
- package/dist/presets/base/components/organizationchart.d.ts +25 -0
- package/dist/presets/base/components/organizationchart.js +29 -0
- package/dist/presets/base/components/organizationchart.js.map +1 -0
- package/dist/presets/base/components/overlaybadge.d.ts +7 -0
- package/dist/presets/base/components/overlaybadge.js +9 -0
- package/dist/presets/base/components/overlaybadge.js.map +1 -0
- package/dist/presets/base/components/paginator.d.ts +30 -0
- package/dist/presets/base/components/paginator.js +32 -0
- package/dist/presets/base/components/paginator.js.map +1 -0
- package/dist/presets/base/components/panel.d.ts +33 -0
- package/dist/presets/base/components/panel.js +33 -0
- package/dist/presets/base/components/panel.js.map +1 -0
- package/dist/presets/base/components/panelmenu.d.ts +37 -0
- package/dist/presets/base/components/panelmenu.js +39 -0
- package/dist/presets/base/components/panelmenu.js.map +1 -0
- package/dist/presets/base/components/password.d.ts +24 -0
- package/dist/presets/base/components/password.js +28 -0
- package/dist/presets/base/components/password.js.map +1 -0
- package/dist/presets/base/components/picklist.d.ts +26 -0
- package/dist/presets/base/components/picklist.js +24 -0
- package/dist/presets/base/components/picklist.js.map +1 -0
- package/dist/presets/base/components/popover.d.ts +12 -0
- package/dist/presets/base/components/popover.js +14 -0
- package/dist/presets/base/components/popover.js.map +1 -0
- package/dist/presets/base/components/progressbar.d.ts +28 -0
- package/dist/presets/base/components/progressbar.js +24 -0
- package/dist/presets/base/components/progressbar.js.map +1 -0
- package/dist/presets/base/components/progressspinner.d.ts +20 -0
- package/dist/presets/base/components/progressspinner.js +16 -0
- package/dist/presets/base/components/progressspinner.js.map +1 -0
- package/dist/presets/base/components/radiobutton.d.ts +36 -0
- package/dist/presets/base/components/radiobutton.js +38 -0
- package/dist/presets/base/components/radiobutton.js.map +1 -0
- package/dist/presets/base/components/radiogroup.d.ts +7 -0
- package/dist/presets/base/components/radiogroup.js +9 -0
- package/dist/presets/base/components/radiogroup.js.map +1 -0
- package/dist/presets/base/components/rating.d.ts +14 -0
- package/dist/presets/base/components/rating.js +18 -0
- package/dist/presets/base/components/rating.js.map +1 -0
- package/dist/presets/base/components/schedule.d.ts +118 -0
- package/dist/presets/base/components/schedule.js +114 -0
- package/dist/presets/base/components/schedule.js.map +1 -0
- package/dist/presets/base/components/scrollpanel.d.ts +12 -0
- package/dist/presets/base/components/scrollpanel.js +14 -0
- package/dist/presets/base/components/scrollpanel.js.map +1 -0
- package/dist/presets/base/components/scrolltop.d.ts +9 -0
- package/dist/presets/base/components/scrolltop.js +11 -0
- package/dist/presets/base/components/scrolltop.js.map +1 -0
- package/dist/presets/base/components/select.d.ts +13 -0
- package/dist/presets/base/components/select.js +15 -0
- package/dist/presets/base/components/select.js.map +1 -0
- package/dist/presets/base/components/selectbutton.d.ts +33 -0
- package/dist/presets/base/components/selectbutton.js +35 -0
- package/dist/presets/base/components/selectbutton.js.map +1 -0
- package/dist/presets/base/components/sidebar.d.ts +39 -0
- package/dist/presets/base/components/sidebar.js +41 -0
- package/dist/presets/base/components/sidebar.js.map +1 -0
- package/dist/presets/base/components/skeleton.d.ts +17 -0
- package/dist/presets/base/components/skeleton.js +19 -0
- package/dist/presets/base/components/skeleton.js.map +1 -0
- package/dist/presets/base/components/slider.d.ts +34 -0
- package/dist/presets/base/components/slider.js +34 -0
- package/dist/presets/base/components/slider.js.map +1 -0
- package/dist/presets/base/components/speeddial.d.ts +18 -0
- package/dist/presets/base/components/speeddial.js +20 -0
- package/dist/presets/base/components/speeddial.js.map +1 -0
- package/dist/presets/base/components/splitbutton.d.ts +9 -0
- package/dist/presets/base/components/splitbutton.js +11 -0
- package/dist/presets/base/components/splitbutton.js.map +1 -0
- package/dist/presets/base/components/splitter.d.ts +33 -0
- package/dist/presets/base/components/splitter.js +31 -0
- package/dist/presets/base/components/splitter.js.map +1 -0
- package/dist/presets/base/components/splitview.d.ts +19 -0
- package/dist/presets/base/components/splitview.js +23 -0
- package/dist/presets/base/components/splitview.js.map +1 -0
- package/dist/presets/base/components/stackpanel.d.ts +6 -0
- package/dist/presets/base/components/stackpanel.js +6 -0
- package/dist/presets/base/components/stackpanel.js.map +1 -0
- package/dist/presets/base/components/stepper.d.ts +37 -0
- package/dist/presets/base/components/stepper.js +39 -0
- package/dist/presets/base/components/stepper.js.map +1 -0
- package/dist/presets/base/components/tabs.d.ts +32 -0
- package/dist/presets/base/components/tabs.js +36 -0
- package/dist/presets/base/components/tabs.js.map +1 -0
- package/dist/presets/base/components/tag.d.ts +19 -0
- package/dist/presets/base/components/tag.js +21 -0
- package/dist/presets/base/components/tag.js.map +1 -0
- package/dist/presets/base/components/taskboard.d.ts +117 -0
- package/dist/presets/base/components/taskboard.js +111 -0
- package/dist/presets/base/components/taskboard.js.map +1 -0
- package/dist/presets/base/components/terminal.d.ts +21 -0
- package/dist/presets/base/components/terminal.js +23 -0
- package/dist/presets/base/components/terminal.js.map +1 -0
- package/dist/presets/base/components/textarea.d.ts +7 -0
- package/dist/presets/base/components/textarea.js +8 -0
- package/dist/presets/base/components/textarea.js.map +1 -0
- package/dist/presets/base/components/tieredmenu.d.ts +34 -0
- package/dist/presets/base/components/tieredmenu.js +32 -0
- package/dist/presets/base/components/tieredmenu.js.map +1 -0
- package/dist/presets/base/components/timeline.d.ts +21 -0
- package/dist/presets/base/components/timeline.js +23 -0
- package/dist/presets/base/components/timeline.js.map +1 -0
- package/dist/presets/base/components/toast.d.ts +35 -0
- package/dist/presets/base/components/toast.js +33 -0
- package/dist/presets/base/components/toast.js.map +1 -0
- package/dist/presets/base/components/togglebutton.d.ts +33 -0
- package/dist/presets/base/components/togglebutton.js +35 -0
- package/dist/presets/base/components/togglebutton.js.map +1 -0
- package/dist/presets/base/components/toggleswitch.d.ts +29 -0
- package/dist/presets/base/components/toggleswitch.js +31 -0
- package/dist/presets/base/components/toggleswitch.js.map +1 -0
- package/dist/presets/base/components/toolbar.d.ts +12 -0
- package/dist/presets/base/components/toolbar.js +14 -0
- package/dist/presets/base/components/toolbar.js.map +1 -0
- package/dist/presets/base/components/tooltip.d.ts +15 -0
- package/dist/presets/base/components/tooltip.js +17 -0
- package/dist/presets/base/components/tooltip.js.map +1 -0
- package/dist/presets/base/components/tree.d.ts +50 -0
- package/dist/presets/base/components/tree.js +52 -0
- package/dist/presets/base/components/tree.js.map +1 -0
- package/dist/presets/base/components/treeselect.d.ts +13 -0
- package/dist/presets/base/components/treeselect.js +17 -0
- package/dist/presets/base/components/treeselect.js.map +1 -0
- package/dist/presets/base/components/treetable.d.ts +67 -0
- package/dist/presets/base/components/treetable.js +67 -0
- package/dist/presets/base/components/treetable.js.map +1 -0
- package/dist/presets/base/components/uniformgrid.d.ts +6 -0
- package/dist/presets/base/components/uniformgrid.js +6 -0
- package/dist/presets/base/components/uniformgrid.js.map +1 -0
- package/dist/presets/base/components/virtualscroller.d.ts +11 -0
- package/dist/presets/base/components/virtualscroller.js +13 -0
- package/dist/presets/base/components/virtualscroller.js.map +1 -0
- package/dist/presets/base/components/wrappanel.d.ts +6 -0
- package/dist/presets/base/components/wrappanel.js +6 -0
- package/dist/presets/base/components/wrappanel.js.map +1 -0
- package/dist/presets/base/index.d.ts +7 -0
- package/dist/presets/base/index.js +17 -0
- package/dist/presets/base/index.js.map +1 -0
- package/dist/presets/base/primitive.d.ts +297 -0
- package/dist/presets/base/primitive.js +302 -0
- package/dist/presets/base/primitive.js.map +1 -0
- package/dist/presets/base/semantic.d.ts +17 -0
- package/dist/presets/base/semantic.js +467 -0
- package/dist/presets/base/semantic.js.map +1 -0
- package/dist/presets/ink.d.ts +11 -0
- package/dist/presets/ink.js +200 -0
- package/dist/presets/ink.js.map +1 -0
- package/dist/presets/prism.d.ts +5 -0
- package/dist/presets/prism.js +12 -0
- package/dist/presets/prism.js.map +1 -0
- package/dist/presets/simple.d.ts +1 -0
- package/dist/presets/simple.js +225 -0
- package/dist/presets/simple.js.map +1 -0
- package/package.json +48 -0
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export interface RGBA {
|
|
2
|
+
r: number;
|
|
3
|
+
g: number;
|
|
4
|
+
b: number;
|
|
5
|
+
a: number;
|
|
6
|
+
}
|
|
7
|
+
/** Reads `#rgb`, `#rgba`, `#rrggbb`, `#rrggbbaa`, `rgb()` and `rgba()`; null for anything else. */
|
|
8
|
+
export declare function parseColor(input: string): RGBA | null;
|
|
9
|
+
export declare function toHex({ r, g, b, a }: RGBA): string;
|
|
10
|
+
/** `a` and `b` blended, with `weight` of `a` (0–1). */
|
|
11
|
+
export declare function mix(a: string, b: string, weight: number): string;
|
|
12
|
+
/** WCAG relative luminance. */
|
|
13
|
+
export declare function luminance(color: string): number;
|
|
14
|
+
export declare function contrastRatio(a: string, b: string): number;
|
|
15
|
+
/** Whichever candidate reads best on `background`. */
|
|
16
|
+
export declare function readableOn(background: string, candidates?: string[]): string;
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
//#region src/engine/color.ts
|
|
2
|
+
/** Reads `#rgb`, `#rgba`, `#rrggbb`, `#rrggbbaa`, `rgb()` and `rgba()`; null for anything else. */
|
|
3
|
+
function parseColor(input) {
|
|
4
|
+
const value = input.trim().toLowerCase();
|
|
5
|
+
const hex = /^#([0-9a-f]{3,8})$/.exec(value);
|
|
6
|
+
if (hex) {
|
|
7
|
+
let digits = hex[1];
|
|
8
|
+
if (digits.length === 3 || digits.length === 4) digits = [...digits].map((d) => d + d).join("");
|
|
9
|
+
if (digits.length !== 6 && digits.length !== 8) return null;
|
|
10
|
+
const n = (i) => parseInt(digits.slice(i, i + 2), 16);
|
|
11
|
+
return {
|
|
12
|
+
r: n(0),
|
|
13
|
+
g: n(2),
|
|
14
|
+
b: n(4),
|
|
15
|
+
a: digits.length === 8 ? n(6) / 255 : 1
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
const fn = /^rgba?\(([^)]+)\)$/.exec(value);
|
|
19
|
+
if (fn) {
|
|
20
|
+
const parts = fn[1].split(/[\s,/]+/).filter(Boolean).map(Number);
|
|
21
|
+
if (parts.length < 3 || parts.some(Number.isNaN)) return null;
|
|
22
|
+
return {
|
|
23
|
+
r: parts[0],
|
|
24
|
+
g: parts[1],
|
|
25
|
+
b: parts[2],
|
|
26
|
+
a: parts[3] ?? 1
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
return null;
|
|
30
|
+
}
|
|
31
|
+
function toHex({ r, g, b, a }) {
|
|
32
|
+
const h = (n) => Math.round(Math.min(255, Math.max(0, n))).toString(16).padStart(2, "0");
|
|
33
|
+
return `#${h(r)}${h(g)}${h(b)}${a < 1 ? h(a * 255) : ""}`;
|
|
34
|
+
}
|
|
35
|
+
/** `a` and `b` blended, with `weight` of `a` (0–1). */
|
|
36
|
+
function mix(a, b, weight) {
|
|
37
|
+
const x = parseColor(a);
|
|
38
|
+
const y = parseColor(b);
|
|
39
|
+
if (!x || !y) throw new Error(`[vitral] cannot mix "${a}" and "${b}"`);
|
|
40
|
+
const w = Math.min(1, Math.max(0, weight));
|
|
41
|
+
return toHex({
|
|
42
|
+
r: x.r * w + y.r * (1 - w),
|
|
43
|
+
g: x.g * w + y.g * (1 - w),
|
|
44
|
+
b: x.b * w + y.b * (1 - w),
|
|
45
|
+
a: x.a * w + y.a * (1 - w)
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
/** WCAG relative luminance. */
|
|
49
|
+
function luminance(color) {
|
|
50
|
+
const c = parseColor(color);
|
|
51
|
+
if (!c) return 0;
|
|
52
|
+
const channel = (v) => {
|
|
53
|
+
const s = v / 255;
|
|
54
|
+
return s <= .03928 ? s / 12.92 : ((s + .055) / 1.055) ** 2.4;
|
|
55
|
+
};
|
|
56
|
+
return .2126 * channel(c.r) + .7152 * channel(c.g) + .0722 * channel(c.b);
|
|
57
|
+
}
|
|
58
|
+
function contrastRatio(a, b) {
|
|
59
|
+
const [hi, lo] = [luminance(a), luminance(b)].sort((x, y) => y - x);
|
|
60
|
+
return (hi + .05) / (lo + .05);
|
|
61
|
+
}
|
|
62
|
+
/** Whichever candidate reads best on `background`. */
|
|
63
|
+
function readableOn(background, candidates = ["#ffffff", "#000000"]) {
|
|
64
|
+
return candidates.reduce((best, c) => contrastRatio(background, c) > contrastRatio(background, best) ? c : best, candidates[0]);
|
|
65
|
+
}
|
|
66
|
+
//#endregion
|
|
67
|
+
export { contrastRatio, luminance, mix, parseColor, readableOn, toHex };
|
|
68
|
+
|
|
69
|
+
//# sourceMappingURL=color.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"color.js","names":[],"sources":["../../src/engine/color.ts"],"sourcesContent":["export interface RGBA {\n r: number;\n g: number;\n b: number;\n a: number;\n}\n\n/** Reads `#rgb`, `#rgba`, `#rrggbb`, `#rrggbbaa`, `rgb()` and `rgba()`; null for anything else. */\nexport function parseColor(input: string): RGBA | null {\n const value = input.trim().toLowerCase();\n const hex = /^#([0-9a-f]{3,8})$/.exec(value);\n if (hex) {\n let digits = hex[1]!;\n if (digits.length === 3 || digits.length === 4) digits = [...digits].map((d) => d + d).join('');\n if (digits.length !== 6 && digits.length !== 8) return null;\n const n = (i: number) => parseInt(digits.slice(i, i + 2), 16);\n return { r: n(0), g: n(2), b: n(4), a: digits.length === 8 ? n(6) / 255 : 1 };\n }\n const fn = /^rgba?\\(([^)]+)\\)$/.exec(value);\n if (fn) {\n const parts = fn[1]!.split(/[\\s,/]+/).filter(Boolean).map(Number);\n if (parts.length < 3 || parts.some(Number.isNaN)) return null;\n return { r: parts[0]!, g: parts[1]!, b: parts[2]!, a: parts[3] ?? 1 };\n }\n return null;\n}\n\nexport function toHex({ r, g, b, a }: RGBA): string {\n const h = (n: number) => Math.round(Math.min(255, Math.max(0, n))).toString(16).padStart(2, '0');\n return `#${h(r)}${h(g)}${h(b)}${a < 1 ? h(a * 255) : ''}`;\n}\n\n/** `a` and `b` blended, with `weight` of `a` (0–1). */\nexport function mix(a: string, b: string, weight: number): string {\n const x = parseColor(a);\n const y = parseColor(b);\n if (!x || !y) throw new Error(`[vitral] cannot mix \"${a}\" and \"${b}\"`);\n const w = Math.min(1, Math.max(0, weight));\n return toHex({ r: x.r * w + y.r * (1 - w), g: x.g * w + y.g * (1 - w), b: x.b * w + y.b * (1 - w), a: x.a * w + y.a * (1 - w) });\n}\n\n/** WCAG relative luminance. */\nexport function luminance(color: string): number {\n const c = parseColor(color);\n if (!c) return 0;\n const channel = (v: number) => {\n const s = v / 255;\n return s <= 0.03928 ? s / 12.92 : ((s + 0.055) / 1.055) ** 2.4;\n };\n return 0.2126 * channel(c.r) + 0.7152 * channel(c.g) + 0.0722 * channel(c.b);\n}\n\nexport function contrastRatio(a: string, b: string): number {\n const [hi, lo] = [luminance(a), luminance(b)].sort((x, y) => y - x) as [number, number];\n return (hi + 0.05) / (lo + 0.05);\n}\n\n/** Whichever candidate reads best on `background`. */\nexport function readableOn(background: string, candidates: string[] = ['#ffffff', '#000000']): string {\n return candidates.reduce((best, c) => (contrastRatio(background, c) > contrastRatio(background, best) ? c : best), candidates[0]!);\n}\n"],"mappings":";;AAQA,SAAgB,WAAW,OAA4B;CACnD,MAAM,QAAQ,MAAM,KAAK,CAAC,CAAC,YAAY;CACvC,MAAM,MAAM,qBAAqB,KAAK,KAAK;CAC3C,IAAI,KAAK;EACL,IAAI,SAAS,IAAI;EACjB,IAAI,OAAO,WAAW,KAAK,OAAO,WAAW,GAAG,SAAS,CAAC,GAAG,MAAM,CAAC,CAAC,KAAK,MAAM,IAAI,CAAC,CAAC,CAAC,KAAK,EAAE;EAC9F,IAAI,OAAO,WAAW,KAAK,OAA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AAI,IAAI,EAAE,KAAK,IAAI;EAAI,GAAG,EAAE,IAAI,IAAI,EAAE,KAAK,IAAI;EAAI,GAAG,EAAE,IAAI,IAAI,EAAE,KAAK,IAAI;CAAG,CAAC;AACnI;;AAGA,SAAgB,UAAU,OAAuB;CAC7C,MAAM,IAAI,WAAW,KAAK;CAC1B,IAAI,CAAC,GAAG,OAAO;CACf,MAAM,WAAW,MAAc;EAC3B,MAAM,IAAI,IAAI;EACd,OAAO,KAAK,SAAU,IAAI,UAAU,IAAI,QAAS,UAAU;CAC/D;CACA,OAAO,QAAS,QAAQ,EAAE,CAAC,IAAI,QAAS,QAAQ,EAAE,CAAC,IAAI,QAAS,QAAQ,EAAE,CAAC;AAC/E;AAEA,SAAgB,cAAc,GAAW,GAAmB;CACxD,MAAM,CAAC,IAAI,MAAM,CAAC,UAAU,CAAC,GAAG,UAAU,CAAC,CAAC,CAAC,CAAC,MAAM,GAAG,MAAM,IAAI,CAAC;CAClE,QAAQ,KAAK,QAAS,KAAK;AAC/B;;AAGA,SAAgB,WAAW,YAAoB,aAAuB,CAAC,WAAW,SAAS,GAAW;CAClG,OAAO,WAAW,QAAQ,MAAM,MAAO,cAAc,YAAY,CAAC,IAAI,cAAc,YAAY,IAAI,IAAI,IAAI,MAAO,WAAW,EAAG;AACrI"}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { type Preset, type ThemeOptions, type TokenTree } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* The custom property a token path compiles to. A `root` segment is dropped, so
|
|
4
|
+
* `button.root.borderRadius` and `button.borderRadius` both name
|
|
5
|
+
* `--vt-button-border-radius`.
|
|
6
|
+
*/
|
|
7
|
+
export declare function varName(path: string | readonly string[], prefix?: string): string;
|
|
8
|
+
/** Turns every `{group.token}` reference in a value into a `var()`. */
|
|
9
|
+
export declare function resolveRefs(value: string, prefix?: string): string;
|
|
10
|
+
/** A token as a `var()`, for inline styles and for code that wants a theme value without hard-coding it. */
|
|
11
|
+
export declare function dt(path: string, prefix?: string): string;
|
|
12
|
+
/** Flattens a token tree into custom properties; used for per-instance overrides as well as whole themes. */
|
|
13
|
+
export declare function flattenTokens(tree: TokenTree | undefined, path?: string[], prefix?: string): Record<string, string>;
|
|
14
|
+
export interface TokenCollision {
|
|
15
|
+
variable: string;
|
|
16
|
+
/** The layers that declare it: `primitive`, `semantic`, or `components.<name>`. */
|
|
17
|
+
layers: string[];
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Custom properties declared by more than one layer. A component's tokens are
|
|
21
|
+
* named after it, so a component whose name is also a semantic group (the
|
|
22
|
+
* `chart` component beside the semantic `chart.1…8` palette) must not use a
|
|
23
|
+
* key the group uses: `components.chart[1]` would compile to `--vt-chart-1`
|
|
24
|
+
* and silently replace a palette colour. Component token trees therefore keep
|
|
25
|
+
* to named (non-numeric) keys, and this check proves it for a preset.
|
|
26
|
+
*/
|
|
27
|
+
export declare function tokenCollisions(preset: Preset, options?: ThemeOptions): TokenCollision[];
|
|
28
|
+
export interface CompiledTheme {
|
|
29
|
+
css: string;
|
|
30
|
+
/** Every custom property of the light scheme (and the scheme-independent ones), with references left as `var()`. */
|
|
31
|
+
light: Record<string, string>;
|
|
32
|
+
/** The properties the dark scheme overrides. */
|
|
33
|
+
dark: Record<string, string>;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Compiles a preset to CSS. Everything is declared on `:root`, and the dark
|
|
37
|
+
* scheme on `:root` plus the dark selector, the same element, because a
|
|
38
|
+
* custom property that refers to another resolves where it is declared: a dark
|
|
39
|
+
* block on some inner element would leave every derived token still light.
|
|
40
|
+
*/
|
|
41
|
+
export declare function compileTheme(preset: Preset, options?: ThemeOptions): CompiledTheme;
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { defaultThemeOptions } from "./types.js";
|
|
2
|
+
import { isObject, toKebab } from "@vitral/core";
|
|
3
|
+
//#region src/engine/compile.ts
|
|
4
|
+
/**
|
|
5
|
+
* The custom property a token path compiles to. A `root` segment is dropped, so
|
|
6
|
+
* `button.root.borderRadius` and `button.borderRadius` both name
|
|
7
|
+
* `--vt-button-border-radius`.
|
|
8
|
+
*/
|
|
9
|
+
function varName(path, prefix = defaultThemeOptions.prefix) {
|
|
10
|
+
return `--${prefix}-${(typeof path === "string" ? path.split(".") : path).filter((p) => p !== "root").map((p) => toKebab(p)).join("-")}`;
|
|
11
|
+
}
|
|
12
|
+
/** Turns every `{group.token}` reference in a value into a `var()`. */
|
|
13
|
+
function resolveRefs(value, prefix = defaultThemeOptions.prefix) {
|
|
14
|
+
return value.replace(/\{([^{}]+)\}/g, (_, ref) => `var(${varName(ref.trim(), prefix)})`);
|
|
15
|
+
}
|
|
16
|
+
/** A token as a `var()`, for inline styles and for code that wants a theme value without hard-coding it. */
|
|
17
|
+
function dt(path, prefix = defaultThemeOptions.prefix) {
|
|
18
|
+
return `var(${varName(path, prefix)})`;
|
|
19
|
+
}
|
|
20
|
+
/** Flattens a token tree into custom properties; used for per-instance overrides as well as whole themes. */
|
|
21
|
+
function flattenTokens(tree, path = [], prefix = defaultThemeOptions.prefix) {
|
|
22
|
+
const out = [];
|
|
23
|
+
walk(tree, path, prefix, out, out, out);
|
|
24
|
+
return Object.fromEntries(out);
|
|
25
|
+
}
|
|
26
|
+
function walk(tree, path, prefix, base, light, dark) {
|
|
27
|
+
if (!tree) return;
|
|
28
|
+
for (const key of Object.keys(tree)) {
|
|
29
|
+
const value = tree[key];
|
|
30
|
+
if (value === void 0) continue;
|
|
31
|
+
if (key === "colorScheme" && isObject(value)) {
|
|
32
|
+
walk(value.light, path, prefix, light, light, light);
|
|
33
|
+
walk(value.dark, path, prefix, dark, dark, dark);
|
|
34
|
+
} else if (isObject(value)) walk(value, [...path, key], prefix, base, light, dark);
|
|
35
|
+
else base.push([varName([...path, key], prefix), resolveRefs(String(value), prefix)]);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Custom properties declared by more than one layer. A component's tokens are
|
|
40
|
+
* named after it, so a component whose name is also a semantic group (the
|
|
41
|
+
* `chart` component beside the semantic `chart.1…8` palette) must not use a
|
|
42
|
+
* key the group uses: `components.chart[1]` would compile to `--vt-chart-1`
|
|
43
|
+
* and silently replace a palette colour. Component token trees therefore keep
|
|
44
|
+
* to named (non-numeric) keys, and this check proves it for a preset.
|
|
45
|
+
*/
|
|
46
|
+
function tokenCollisions(preset, options = {}) {
|
|
47
|
+
const { prefix } = {
|
|
48
|
+
...defaultThemeOptions,
|
|
49
|
+
...options
|
|
50
|
+
};
|
|
51
|
+
const owners = /* @__PURE__ */ new Map();
|
|
52
|
+
const add = (layer, tree, path) => {
|
|
53
|
+
const entries = [];
|
|
54
|
+
walk(tree, path, prefix, entries, entries, entries);
|
|
55
|
+
for (const [name] of entries) {
|
|
56
|
+
if (!owners.has(name)) owners.set(name, /* @__PURE__ */ new Set());
|
|
57
|
+
owners.get(name).add(layer);
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
add("primitive", preset.primitive, []);
|
|
61
|
+
add("semantic", preset.semantic, []);
|
|
62
|
+
for (const [name, tokens] of Object.entries(preset.components ?? {})) add(`components.${name}`, tokens, [name]);
|
|
63
|
+
return [...owners].filter(([, layers]) => layers.size > 1).map(([variable, layers]) => ({
|
|
64
|
+
variable,
|
|
65
|
+
layers: [...layers]
|
|
66
|
+
}));
|
|
67
|
+
}
|
|
68
|
+
function darkBlock(selector, body) {
|
|
69
|
+
if (selector === "system") return `@media (prefers-color-scheme: dark) {\n:root {${body}\n color-scheme: dark;\n}\n}\n`;
|
|
70
|
+
return `${selector.startsWith(".") || selector.startsWith("[") ? `:root${selector}` : selector} {${body}\n color-scheme: dark;\n}\n`;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Compiles a preset to CSS. Everything is declared on `:root`, and the dark
|
|
74
|
+
* scheme on `:root` plus the dark selector, the same element, because a
|
|
75
|
+
* custom property that refers to another resolves where it is declared: a dark
|
|
76
|
+
* block on some inner element would leave every derived token still light.
|
|
77
|
+
*/
|
|
78
|
+
function compileTheme(preset, options = {}) {
|
|
79
|
+
const { prefix, darkModeSelector, cssLayer } = {
|
|
80
|
+
...defaultThemeOptions,
|
|
81
|
+
...options
|
|
82
|
+
};
|
|
83
|
+
const base = [];
|
|
84
|
+
const light = [];
|
|
85
|
+
const dark = [];
|
|
86
|
+
walk(preset.primitive, [], prefix, base, light, dark);
|
|
87
|
+
walk(preset.semantic, [], prefix, base, light, dark);
|
|
88
|
+
for (const [name, tokens] of Object.entries(preset.components ?? {})) walk(tokens, [name], prefix, base, light, dark);
|
|
89
|
+
const body = (entries) => entries.map(([n, v]) => `\n ${n}: ${v};`).join("");
|
|
90
|
+
let css = `:root {${body(base)}${body(light)}\n color-scheme: light;\n}\n`;
|
|
91
|
+
if (darkModeSelector && dark.length > 0) css += darkBlock(darkModeSelector, body(dark));
|
|
92
|
+
if (cssLayer) css = `@layer ${cssLayer} {\n${css}}\n`;
|
|
93
|
+
return {
|
|
94
|
+
css,
|
|
95
|
+
light: Object.fromEntries([...base, ...light]),
|
|
96
|
+
dark: Object.fromEntries(dark)
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
//#endregion
|
|
100
|
+
export { compileTheme, dt, flattenTokens, resolveRefs, tokenCollisions, varName };
|
|
101
|
+
|
|
102
|
+
//# sourceMappingURL=compile.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"compile.js","names":[],"sources":["../../src/engine/compile.ts"],"sourcesContent":["import { isObject, toKebab } from '@vitral/core';\nimport { defaultThemeOptions, type Preset, type ThemeOptions, type TokenTree } from './types';\n\ntype Entry = [name: string, value: string];\n\n/**\n * The custom property a token path compiles to. A `root` segment is dropped, so\n * `button.root.borderRadius` and `button.borderRadius` both name\n * `--vt-button-border-radius`.\n */\nexport function varName(path: string | readonly string[], prefix = defaultThemeOptions.prefix): string {\n const parts = typeof path === 'string' ? path.split('.') : path;\n return `--${prefix}-${parts\n .filter((p) => p !== 'root')\n .map((p) => toKebab(p))\n .join('-')}`;\n}\n\n/** Turns every `{group.token}` reference in a value into a `var()`. */\nexport function resolveRefs(value: string, prefix = defaultThemeOptions.prefix): string {\n return value.replace(/\\{([^{}]+)\\}/g, (_, ref: string) => `var(${varName(ref.trim(), prefix)})`);\n}\n\n/** A token as a `var()`, for inline styles and for code that wants a theme value without hard-coding it. */\nexport function dt(path: string, prefix = defaultThemeOptions.prefix): string {\n return `var(${varName(path, prefix)})`;\n}\n\n/** Flattens a token tree into custom properties; used for per-instance overrides as well as whole themes. */\nexport function flattenTokens(tree: TokenTree | undefined, path: string[] = [], prefix = defaultThemeOptions.prefix): Record<string, string> {\n const out: Entry[] = [];\n walk(tree, path, prefix, out, out, out);\n return Object.fromEntries(out);\n}\n\nfunction walk(tree: TokenTree | undefined, path: string[], prefix: string, base: Entry[], light: Entry[], dark: Entry[]): void {\n if (!tree) return;\n for (const key of Object.keys(tree)) {\n const value = tree[key];\n if (value === undefined) continue;\n if (key === 'colorScheme' && isObject(value)) {\n walk(value.light as TokenTree | undefined, path, prefix, light, light, light);\n walk(value.dark as TokenTree | undefined, path, prefix, dark, dark, dark);\n } else if (isObject(value)) {\n walk(value as TokenTree, [...path, key], prefix, base, light, dark);\n } else {\n base.push([varName([...path, key], prefix), resolveRefs(String(value), prefix)]);\n }\n }\n}\n\nexport interface TokenCollision {\n variable: string;\n /** The layers that declare it: `primitive`, `semantic`, or `components.<name>`. */\n layers: string[];\n}\n\n/**\n * Custom properties declared by more than one layer. A component's tokens are\n * named after it, so a component whose name is also a semantic group (the\n * `chart` component beside the semantic `chart.1…8` palette) must not use a\n * key the group uses: `components.chart[1]` would compile to `--vt-chart-1`\n * and silently replace a palette colour. Component token trees therefore keep\n * to named (non-numeric) keys, and this check proves it for a preset.\n */\nexport function tokenCollisions(preset: Preset, options: ThemeOptions = {}): TokenCollision[] {\n const { prefix } = { ...defaultThemeOptions, ...options };\n const owners = new Map<string, Set<string>>();\n const add = (layer: string, tree: TokenTree | undefined, path: string[]) => {\n const entries: Entry[] = [];\n walk(tree, path, prefix, entries, entries, entries);\n for (const [name] of entries) {\n if (!owners.has(name)) owners.set(name, new Set());\n owners.get(name)!.add(layer);\n }\n };\n add('primitive', preset.primitive, []);\n add('semantic', preset.semantic, []);\n for (const [name, tokens] of Object.entries(preset.components ?? {})) add(`components.${name}`, tokens, [name]);\n return [...owners].filter(([, layers]) => layers.size > 1).map(([variable, layers]) => ({ variable, layers: [...layers] }));\n}\n\nexport interface CompiledTheme {\n css: string;\n /** Every custom property of the light scheme (and the scheme-independent ones), with references left as `var()`. */\n light: Record<string, string>;\n /** The properties the dark scheme overrides. */\n dark: Record<string, string>;\n}\n\nfunction darkBlock(selector: string, body: string): string {\n if (selector === 'system') return `@media (prefers-color-scheme: dark) {\\n:root {${body}\\n color-scheme: dark;\\n}\\n}\\n`;\n const scoped = selector.startsWith('.') || selector.startsWith('[') ? `:root${selector}` : selector;\n return `${scoped} {${body}\\n color-scheme: dark;\\n}\\n`;\n}\n\n/**\n * Compiles a preset to CSS. Everything is declared on `:root`, and the dark\n * scheme on `:root` plus the dark selector, the same element, because a\n * custom property that refers to another resolves where it is declared: a dark\n * block on some inner element would leave every derived token still light.\n */\nexport function compileTheme(preset: Preset, options: ThemeOptions = {}): CompiledTheme {\n const { prefix, darkModeSelector, cssLayer } = { ...defaultThemeOptions, ...options };\n const base: Entry[] = [];\n const light: Entry[] = [];\n const dark: Entry[] = [];\n walk(preset.primitive, [], prefix, base, light, dark);\n walk(preset.semantic, [], prefix, base, light, dark);\n for (const [name, tokens] of Object.entries(preset.components ?? {})) walk(tokens, [name], prefix, base, light, dark);\n\n const body = (entries: Entry[]) => entries.map(([n, v]) => `\\n ${n}: ${v};`).join('');\n let css = `:root {${body(base)}${body(light)}\\n color-scheme: light;\\n}\\n`;\n if (darkModeSelector && dark.length > 0) css += darkBlock(darkModeSelector, body(dark));\n if (cssLayer) css = `@layer ${cssLayer} {\\n${css}}\\n`;\n return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gB,CAAC;CACvB,MAAM,QAAiB,CAAC;CACxB,MAAM,OAAgB,CAAC;CACvB,KAAK,OAAO,WAAW,CAAC,GAAG,QAAQ,MAAM,OAAO,IAAI;CACpD,KAAK,OAAO,UAAU,CAAC,GAAG,QAAQ,MAAM,OAAO,IAAI;CACnD,KAAK,MAAM,CAAC,MAAM,WAAW,OAAO,QAAQ,OAAO,cAAc,CAAC,CAAC,GAAG,KAAK,QAAQ,CAAC,IAAI,GAAG,QAAQ,MAAM,OAAO,IAAI;CAEpH,MAAM,QAAQ,YAAqB,QAAQ,KAAK,CAAC,GAAG,OAAO,OAAO,EAAE,IAAI,EAAE,EAAE,CAAC,CAAC,KAAK,EAAE;CACrF,IAAI,MAAM,UAAU,KAAK,IAAI,IAAI,KAAK,KAAK,EAAE;CAC7C,IAAI,oBAAoB,KAAK,SAAS,GAAG,OAAO,UAAU,kBAAkB,KAAK,IAAI,CAAC;CACtF,IAAI,UAAU,MAAM,UAAU,SAAS,MAAM,IAAI;CACjD,OAAO;EAAE;EAAK,OAAO,OAAO,YAAY,CAAC,GAAG,MAAM,GAAG,KAAK,CAAC;EAAG,MAAM,OAAO,YAAY,IAAI;CAAE;AACjG"}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { type ColorScheme } from './scheme';
|
|
2
|
+
import { type Preset, type ThemeOptions } from './types';
|
|
3
|
+
export interface ThemeState {
|
|
4
|
+
preset: Preset;
|
|
5
|
+
colorScheme: ColorScheme;
|
|
6
|
+
/** The scheme actually showing: `colorScheme` with `'system'` resolved. */
|
|
7
|
+
dark: boolean;
|
|
8
|
+
}
|
|
9
|
+
export interface ThemeManagerOptions extends ThemeOptions {
|
|
10
|
+
preset: Preset;
|
|
11
|
+
colorScheme?: ColorScheme;
|
|
12
|
+
/** Nonce for the injected `<style>`, under a strict Content-Security-Policy. */
|
|
13
|
+
nonce?: string;
|
|
14
|
+
/** Remember the chosen scheme in localStorage under this key. Off by default. */
|
|
15
|
+
storageKey?: string | false;
|
|
16
|
+
}
|
|
17
|
+
export interface ThemeManager {
|
|
18
|
+
readonly options: Required<ThemeOptions>;
|
|
19
|
+
/** The localStorage key the scheme is remembered under, or false. `colorSchemeScript` has to agree with it. */
|
|
20
|
+
readonly storageKey: string | false;
|
|
21
|
+
getState(): ThemeState;
|
|
22
|
+
setPreset(preset: Preset): void;
|
|
23
|
+
/** Merges a partial preset over the current one: a new primary colour, a rounder radius. */
|
|
24
|
+
extendPreset(partial: Preset): void;
|
|
25
|
+
setColorScheme(scheme: ColorScheme): void;
|
|
26
|
+
toggleDark(): void;
|
|
27
|
+
subscribe(listener: (state: ThemeState) => void): () => void;
|
|
28
|
+
/** Injects the stylesheet and starts following the system scheme. Safe to call on the server, where it does nothing. */
|
|
29
|
+
mount(): void;
|
|
30
|
+
destroy(): void;
|
|
31
|
+
css(): string;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Owns the theme at runtime: compiles the preset into one `<style>` element,
|
|
35
|
+
* applies the dark selector to `<html>`, and tells subscribers when anything
|
|
36
|
+
* changes. It knows nothing about any framework: the Vue plugin wraps it, and a
|
|
37
|
+
* React or Angular adapter would wrap the same object.
|
|
38
|
+
*/
|
|
39
|
+
export declare function createThemeManager(init: ThemeManagerOptions): ThemeManager;
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import { defaultThemeOptions } from "./types.js";
|
|
2
|
+
import { compileTheme } from "./compile.js";
|
|
3
|
+
import { definePreset } from "./preset.js";
|
|
4
|
+
import { applyDarkModeTo } from "./scheme.js";
|
|
5
|
+
import { isClient } from "@vitral/core";
|
|
6
|
+
//#region src/engine/manager.ts
|
|
7
|
+
var STYLE_ATTR = "data-vitral-theme";
|
|
8
|
+
/**
|
|
9
|
+
* Owns the theme at runtime: compiles the preset into one `<style>` element,
|
|
10
|
+
* applies the dark selector to `<html>`, and tells subscribers when anything
|
|
11
|
+
* changes. It knows nothing about any framework: the Vue plugin wraps it, and a
|
|
12
|
+
* React or Angular adapter would wrap the same object.
|
|
13
|
+
*/
|
|
14
|
+
function createThemeManager(init) {
|
|
15
|
+
const options = {
|
|
16
|
+
prefix: init.prefix ?? defaultThemeOptions.prefix,
|
|
17
|
+
darkModeSelector: init.darkModeSelector ?? defaultThemeOptions.darkModeSelector,
|
|
18
|
+
cssLayer: init.cssLayer ?? defaultThemeOptions.cssLayer
|
|
19
|
+
};
|
|
20
|
+
let preset = init.preset;
|
|
21
|
+
let colorScheme = readStored(init.storageKey) ?? init.colorScheme ?? "system";
|
|
22
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
23
|
+
let styleEl = null;
|
|
24
|
+
let media = null;
|
|
25
|
+
const systemDark = () => isClient && typeof matchMedia === "function" ? matchMedia("(prefers-color-scheme: dark)").matches : false;
|
|
26
|
+
const isDark = () => colorScheme === "dark" || colorScheme === "system" && systemDark();
|
|
27
|
+
const state = () => ({
|
|
28
|
+
preset,
|
|
29
|
+
colorScheme,
|
|
30
|
+
dark: isDark()
|
|
31
|
+
});
|
|
32
|
+
const css = () => compileTheme(preset, options).css;
|
|
33
|
+
const notify = () => {
|
|
34
|
+
const s = state();
|
|
35
|
+
listeners.forEach((l) => l(s));
|
|
36
|
+
};
|
|
37
|
+
const applyScheme = () => {
|
|
38
|
+
if (!isClient) return;
|
|
39
|
+
applyDarkModeTo(document.documentElement, isDark(), options.darkModeSelector);
|
|
40
|
+
};
|
|
41
|
+
const writeStyle = () => {
|
|
42
|
+
if (styleEl) styleEl.textContent = css();
|
|
43
|
+
};
|
|
44
|
+
const onSystemChange = () => {
|
|
45
|
+
if (colorScheme !== "system") return;
|
|
46
|
+
applyScheme();
|
|
47
|
+
notify();
|
|
48
|
+
};
|
|
49
|
+
return {
|
|
50
|
+
options,
|
|
51
|
+
storageKey: init.storageKey ?? false,
|
|
52
|
+
getState: state,
|
|
53
|
+
css,
|
|
54
|
+
setPreset(next) {
|
|
55
|
+
preset = next;
|
|
56
|
+
writeStyle();
|
|
57
|
+
notify();
|
|
58
|
+
},
|
|
59
|
+
extendPreset(partial) {
|
|
60
|
+
preset = definePreset(preset, partial);
|
|
61
|
+
writeStyle();
|
|
62
|
+
notify();
|
|
63
|
+
},
|
|
64
|
+
setColorScheme(scheme) {
|
|
65
|
+
colorScheme = scheme;
|
|
66
|
+
store(init.storageKey, scheme);
|
|
67
|
+
applyScheme();
|
|
68
|
+
notify();
|
|
69
|
+
},
|
|
70
|
+
toggleDark() {
|
|
71
|
+
this.setColorScheme(isDark() ? "light" : "dark");
|
|
72
|
+
},
|
|
73
|
+
subscribe(listener) {
|
|
74
|
+
listeners.add(listener);
|
|
75
|
+
return () => listeners.delete(listener);
|
|
76
|
+
},
|
|
77
|
+
mount() {
|
|
78
|
+
if (!isClient || styleEl) return;
|
|
79
|
+
styleEl = document.head.querySelector(`style[${STYLE_ATTR}]`);
|
|
80
|
+
if (!styleEl) {
|
|
81
|
+
styleEl = document.createElement("style");
|
|
82
|
+
styleEl.setAttribute(STYLE_ATTR, "");
|
|
83
|
+
if (init.nonce) styleEl.nonce = init.nonce;
|
|
84
|
+
document.head.prepend(styleEl);
|
|
85
|
+
}
|
|
86
|
+
writeStyle();
|
|
87
|
+
applyScheme();
|
|
88
|
+
if (typeof matchMedia === "function") {
|
|
89
|
+
media = matchMedia("(prefers-color-scheme: dark)");
|
|
90
|
+
media.addEventListener?.("change", onSystemChange);
|
|
91
|
+
}
|
|
92
|
+
},
|
|
93
|
+
destroy() {
|
|
94
|
+
media?.removeEventListener?.("change", onSystemChange);
|
|
95
|
+
media = null;
|
|
96
|
+
styleEl?.remove();
|
|
97
|
+
styleEl = null;
|
|
98
|
+
listeners.clear();
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
function readStored(key) {
|
|
103
|
+
if (!key || !isClient) return null;
|
|
104
|
+
try {
|
|
105
|
+
const value = localStorage.getItem(key);
|
|
106
|
+
return value === "light" || value === "dark" || value === "system" ? value : null;
|
|
107
|
+
} catch {
|
|
108
|
+
return null;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
function store(key, scheme) {
|
|
112
|
+
if (!key || !isClient) return;
|
|
113
|
+
try {
|
|
114
|
+
localStorage.setItem(key, scheme);
|
|
115
|
+
} catch {}
|
|
116
|
+
}
|
|
117
|
+
//#endregion
|
|
118
|
+
export { createThemeManager };
|
|
119
|
+
|
|
120
|
+
//# sourceMappingURL=manager.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"manager.js","names":[],"sources":["../../src/engine/manager.ts"],"sourcesContent":["import { isClient } from '@vitral/core';\nimport { compileTheme } from './compile';\nimport { definePreset } from './preset';\nimport { applyDarkModeTo, type ColorScheme } from './scheme';\nimport { defaultThemeOptions, type Preset, type ThemeOptions } from './types';\n\nexport interface ThemeState {\n preset: Preset;\n colorScheme: ColorScheme;\n /** The scheme actually showing: `colorScheme` with `'system'` resolved. */\n dark: boolean;\n}\n\nexport interface ThemeManagerOptions extends ThemeOptions {\n preset: Preset;\n colorScheme?: ColorScheme;\n /** Nonce for the injected `<style>`, under a strict Content-Security-Policy. */\n nonce?: string;\n /** Remember the chosen scheme in localStorage under this key. Off by default. */\n storageKey?: string | false;\n}\n\nexport interface ThemeManager {\n readonly options: Required<ThemeOptions>;\n /** The localStorage key the scheme is remembered under, or false. `colorSchemeScript` has to agree with it. */\n readonly storageKey: string | false;\n getState(): ThemeState;\n setPreset(preset: Preset): void;\n /** Merges a partial preset over the current one: a new primary colour, a rounder radius. */\n extendPreset(partial: Preset): void;\n setColorScheme(scheme: ColorScheme): void;\n toggleDark(): void;\n subscribe(listener: (state: ThemeState) => void): () => void;\n /** Injects the stylesheet and starts following the system scheme. Safe to call on the server, where it does nothing. */\n mount(): void;\n destroy(): void;\n css(): string;\n}\n\nconst STYLE_ATTR = 'data-vitral-theme';\n\n/**\n * Owns the theme at runtime: compiles the preset into one `<style>` element,\n * applies the dark selector to `<html>`, and tells subscribers when anything\n * changes. It knows nothing about any framework: the Vue plugin wraps it, and a\n * React or Angular adapter would wrap the same object.\n */\nexport function createThemeManager(init: ThemeManagerOptions): ThemeManager {\n const options: Required<ThemeOptions> = {\n prefix: init.prefix ?? defaultThemeOptions.prefix,\n darkModeSelector: init.darkModeSelector ?? defaultThemeOptions.darkModeSelector,\n cssLayer: init.cssLayer ?? defaultThemeOptions.cssLayer\n };\n let preset = init.preset;\n let colorScheme: ColorScheme = readStored(init.storageKey) ?? init.colorScheme ?? 'system';\n const listeners = new Set<(state: ThemeState) => void>();\n let styleEl: HTMLStyleElement | null = null;\n let media: MediaQueryList | null = null;\n\n const systemDark = () => (isClient && typeof matchMedia === 'function' ? matchMedia('(prefers-color-scheme: dark)').matches : false);\n const isDark = () => colorScheme === 'dark' || (colorScheme === 'system' && systemDark());\n const state = (): ThemeState => ({ preset, colorScheme, dark: isDark() });\n const css = () => compileTheme(preset, options).css;\n\n const notify = () => {\n const s = state();\n listeners.forEach((l) => l(s));\n };\n\n const applyScheme = () => {\n if (!isClient) return;\n applyDarkModeTo(document.documentElement, isDark(), options.darkModeSelector);\n };\n\n const writeStyle = () => {\n if (styleEl) styleEl.textContent = css();\n };\n\n const onSystemChange = () => {\n if (colorScheme !== 'system') return;\n applyScheme();\n notify();\n };\n\n return {\n options,\n storageKey: init.storageKey ?? false,\n getState: state,\n css,\n setPreset(next) {\n preset = next;\n writeStyle();\n notify();\n },\n extendPreset(partial) {\n preset = definePreset(preset, partial);\n writeStyle();\n notify();\n },\n setColorScheme(scheme) {\n colorScheme = scheme;\n store(init.storageKey, scheme);\n applyScheme();\n notify();\n },\n toggleDark() {\n this.setColorScheme(isDark() ? 'light' : 'dark');\n },\n subscribe(listener) {\n listeners.add(listener);\n return () => listeners.delete(listener);\n },\n mount() {\n if (!isClient || styleEl) return;\n styleEl = document.head.querySelector<HTMLStyleElement>(`style[${STYLE_ATTR}]`);\n if (!styleEl) {\n styleEl = document.createElement('style');\n styleEl.setAttribute(STYLE_ATTR, '');\n if (init.nonce) styleEl.nonce = init.nonce;\n document.head.prepend(styleEl);\n }\n writeStyle();\n applyScheme();\n if (typeof matchMedia === 'function') {\n media = matchMedia('(prefers-color-scheme: dark)');\n media.addEventListener?.('change', onSystemChange);\n }\n },\n destroy() {\n media?.removeEventListener?.('change', onSystemChange);\n media = null;\n styleEl?.remove();\n styleEl = null;\n listeners.clear();\n }\n };\n}\n\nfunction readStored(key: string | false | undefined): ColorScheme | null {\n if (!key || !isClient) return null;\n try {\n const value = localStorage.getItem(key);\n return value === 'light' || value === 'dark' || value === 'system' ? value : null;\n } catch {\n return null;\n }\n}\n\nfunction store(key: string | false | undefined, scheme: ColorScheme): void {\n if (!key || !isClient) return;\n try {\n localStorage.setItem(key, scheme);\n } catch {\n /* storage blocked: the choice simply is not remembered */\n }\n}\n"],"mappings":";;;;;;AAuCA,IAAM,aAAa;;;;;;;AAQnB,SAAgB,mBAAmB,MAAyC;CACxE,MAAM,UAAkC;EACpC,QAAQ,KAAK,UAAU,oBAAoB;EAC3C,kBAAkB,KAAK,oBAAoB,oBAAoB;EAC/D,UAAU,KAAK,YAAY,oBAAoB;CACnD;CACA,IAAI,SAAS,KAAK;CAClB,IAAI,cAA2B,WAAW,KAAK,UAAU,KAAK,KAAK,eAAe;CAClF,MAAM,4BAAY,IAAI,IAAiC;CACvD,IAAI,UAAmC;CACvC,IAAI,QAA+B;CAEnC,MAAM,mBAAoB,YAAY,OAAO,eAAe,aAAa,WAAW,8BAA8B,CAAC,CAAC,UAAU;CAC9H,MAAM,eAAe,gBAAgB,UAAW,gBAAgB,YAAY,WAAW;CACvF,MAAM,eAA2B;EAAE;EAAQ;EAAa,MAAM,OAAO;CAAE;CACvE,MAAM,YAAY,aAAa,QAAQ,OAAO,CAAC,CAAC;CAEhD,MAAM,eAAe;EACjB,MAAM,IAAI,MAAM;EAChB,UAAU,SAAS,MAAM,EAAE,CAAC,CAAC;CACjC;CAEA,MAAM,oBAAoB;EACtB,IAAI,CAAC,UAAU;EACf,gBAAgB,SAAS,iBAAiB,OAAO,GAAG,QAA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,UAAU,WAAW,QAAQ;CACjF,QAAQ;EACJ,OAAO;CACX;AACJ;AAEA,SAAS,MAAM,KAAiC,QAA2B;CACvE,IAAI,CAAC,OAAO,CAAC,UAAU;CACvB,IAAI;EACA,aAAa,QAAQ,KAAK,MAAM;CACpC,QAAQ,CAER;AACJ"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export declare const SHADES: readonly [50, 100, 200, 300, 400, 500, 600, 700, 800, 900, 950];
|
|
2
|
+
export type Shade = (typeof SHADES)[number];
|
|
3
|
+
export type Palette = Record<Shade, string>;
|
|
4
|
+
/**
|
|
5
|
+
* Eleven shades from one colour: 500 is the colour itself, lighter shades mix
|
|
6
|
+
* toward white and darker ones toward black.
|
|
7
|
+
*
|
|
8
|
+
* Passing a reference such as `'{emerald}'` instead returns references to that
|
|
9
|
+
* palette's shades (`{emerald.50}` … `{emerald.950}`), which is how a preset
|
|
10
|
+
* says "primary is emerald" without copying eleven values.
|
|
11
|
+
*/
|
|
12
|
+
export declare function palette(color: string): Palette;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { mix, parseColor, toHex } from "./color.js";
|
|
2
|
+
//#region src/engine/palette.ts
|
|
3
|
+
var SHADES = [
|
|
4
|
+
50,
|
|
5
|
+
100,
|
|
6
|
+
200,
|
|
7
|
+
300,
|
|
8
|
+
400,
|
|
9
|
+
500,
|
|
10
|
+
600,
|
|
11
|
+
700,
|
|
12
|
+
800,
|
|
13
|
+
900,
|
|
14
|
+
950
|
|
15
|
+
];
|
|
16
|
+
/**
|
|
17
|
+
* Eleven shades from one colour: 500 is the colour itself, lighter shades mix
|
|
18
|
+
* toward white and darker ones toward black.
|
|
19
|
+
*
|
|
20
|
+
* Passing a reference such as `'{emerald}'` instead returns references to that
|
|
21
|
+
* palette's shades (`{emerald.50}` … `{emerald.950}`), which is how a preset
|
|
22
|
+
* says "primary is emerald" without copying eleven values.
|
|
23
|
+
*/
|
|
24
|
+
function palette(color) {
|
|
25
|
+
const ref = /^\{(.+)\}$/.exec(color.trim());
|
|
26
|
+
if (ref) return Object.fromEntries(SHADES.map((shade) => [shade, `{${ref[1]}.${shade}}`]));
|
|
27
|
+
const parsed = parseColor(color);
|
|
28
|
+
if (!parsed) throw new Error(`[vitral] palette() needs a hex or rgb colour, got "${color}"`);
|
|
29
|
+
const base = toHex(parsed);
|
|
30
|
+
return Object.fromEntries(SHADES.map((shade, i) => {
|
|
31
|
+
if (i < 5) return [shade, mix("#ffffff", base, (5 - i) * .19)];
|
|
32
|
+
if (i === 5) return [shade, base];
|
|
33
|
+
return [shade, mix("#000000", base, (i - 5) * .15)];
|
|
34
|
+
}));
|
|
35
|
+
}
|
|
36
|
+
//#endregion
|
|
37
|
+
export { SHADES, palette };
|
|
38
|
+
|
|
39
|
+
//# sourceMappingURL=palette.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"palette.js","names":[],"sources":["../../src/engine/palette.ts"],"sourcesContent":["import { mix, parseColor, toHex } from './color';\n\nexport const SHADES = [50, 100, 200, 300, 400, 500, 600, 700, 800, 900, 950] as const;\n\nexport type Shade = (typeof SHADES)[number];\nexport type Palette = Record<Shade, string>;\n\n/**\n * Eleven shades from one colour: 500 is the colour itself, lighter shades mix\n * toward white and darker ones toward black.\n *\n * Passing a reference such as `'{emerald}'` instead returns references to that\n * palette's shades (`{emerald.50}` … `{emerald.950}`), which is how a preset\n * says \"primary is emerald\" without copying eleven values.\n */\nexport function palette(color: string): Palette {\n const ref = /^\\{(.+)\\}$/.exec(color.trim());\n if (ref) return Object.fromEntries(SHADES.map((shade) => [shade, `{${ref[1]}.${shade}}`])) as Palette;\n const parsed = parseColor(color);\n if (!parsed) throw new Error(`[vitral] palette() needs a hex or rgb colour, got \"${color}\"`);\n const base = toHex(parsed);\n return Object.fromEntries(\n SHADES.map((shade, i) => {\n if (i < 5) return [shade, mix('#ffffff', base, (5 - i) * 0.19)];\n if (i === 5) return [shade, base];\n return [shade, mix('#000000', base, (i - 5) * 0.15)];\n })\n ) as Palette;\n}\n"],"mappings":";;AAEA,IAAa,SAAS;CAAC;CAAI;CAAK;CAAK;CAAK;CAAK;CAAK;CAAK;CAAK;CAAK;CAAK;AAAG;;;;;;;;;AAa3E,SAAgB,QAAQ,OAAwB;CAC5C,MAAM,MAAM,aAAa,KAAK,MAAM,KAAK,CAAC;CAC1C,IAAI,KAAK,OAAO,OAAO,YAAY,OAAO,KAAK,UAAU,CAAC,OAAO,IAAI,IAAI,GAAG,GAAG,MAAM,EAAE,CAAC,CAAC;CACzF,MAAM,SAAS,WAAW,KAAK;CAC/B,IAAI,CAAC,QAAQ,MAAM,IAAI,MAAM,sDAAsD,MAAM,EAAE;CAC3F,MAAM,OAAO,MAAM,MAAM;CACzB,OAAO,OAAO,YACV,OAAO,KAAK,OAAO,MAAM;EACrB,IAAI,IAAI,GAAG,OAAO,CAAC,OAAO,IAAI,WAAW,OAAO,IAAI,KAAK,GAAI,CAAC;EAC9D,IAAI,MAAM,GAAG,OAAO,CAAC,OAAO,IAAI;EAChC,OAAO,CAAC,OAAO,IAAI,WAAW,OAAO,IAAI,KAAK,GAAI,CAAC;CACvD,CAAC,CACL;AACJ"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { type Palette } from './palette';
|
|
2
|
+
import type { Preset } from './types';
|
|
3
|
+
/** A preset built on another: later arguments override earlier ones, key by key. */
|
|
4
|
+
export declare function definePreset(base: Preset, ...overrides: Preset[]): Preset;
|
|
5
|
+
/**
|
|
6
|
+
* Replaces the primary palette, with a colour, a `{palette}` reference or
|
|
7
|
+
* eleven explicit shades, and re-derives the scheme's primary fills from it.
|
|
8
|
+
* The second half matters: a preset such as Avalonia pins `primary.color` to an
|
|
9
|
+
* exact accent, and swapping only the palette would leave that pin in place.
|
|
10
|
+
*/
|
|
11
|
+
export declare function updatePrimaryPalette(preset: Preset, primary: string | Palette): Preset;
|
|
12
|
+
/** Replaces the neutral surface palette for either scheme, or both. */
|
|
13
|
+
export declare function updateSurfacePalette(preset: Preset, surface: {
|
|
14
|
+
light?: string | Palette;
|
|
15
|
+
dark?: string | Palette;
|
|
16
|
+
}): Preset;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { palette } from "./palette.js";
|
|
2
|
+
import { severityDark, severityLight } from "../presets/base/semantic.js";
|
|
3
|
+
import { deepMerge } from "@vitral/core";
|
|
4
|
+
//#region src/engine/preset.ts
|
|
5
|
+
/** A preset built on another: later arguments override earlier ones, key by key. */
|
|
6
|
+
function definePreset(base, ...overrides) {
|
|
7
|
+
return deepMerge(base, ...overrides);
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Replaces the primary palette, with a colour, a `{palette}` reference or
|
|
11
|
+
* eleven explicit shades, and re-derives the scheme's primary fills from it.
|
|
12
|
+
* The second half matters: a preset such as Avalonia pins `primary.color` to an
|
|
13
|
+
* exact accent, and swapping only the palette would leave that pin in place.
|
|
14
|
+
*/
|
|
15
|
+
function updatePrimaryPalette(preset, primary) {
|
|
16
|
+
return definePreset(preset, { semantic: {
|
|
17
|
+
primary: typeof primary === "string" ? palette(primary) : primary,
|
|
18
|
+
colorScheme: {
|
|
19
|
+
light: { primary: severityLight("primary") },
|
|
20
|
+
dark: { primary: severityDark("primary") }
|
|
21
|
+
}
|
|
22
|
+
} });
|
|
23
|
+
}
|
|
24
|
+
/** Replaces the neutral surface palette for either scheme, or both. */
|
|
25
|
+
function updateSurfacePalette(preset, surface) {
|
|
26
|
+
const resolve = (value) => value === void 0 ? void 0 : typeof value === "string" ? palette(value) : value;
|
|
27
|
+
return definePreset(preset, { semantic: { colorScheme: {
|
|
28
|
+
light: surface.light ? { surface: resolve(surface.light) } : void 0,
|
|
29
|
+
dark: surface.dark ? { surface: resolve(surface.dark) } : void 0
|
|
30
|
+
} } });
|
|
31
|
+
}
|
|
32
|
+
//#endregion
|
|
33
|
+
export { definePreset, updatePrimaryPalette, updateSurfacePalette };
|
|
34
|
+
|
|
35
|
+
//# sourceMappingURL=preset.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"preset.js","names":[],"sources":["../../src/engine/preset.ts"],"sourcesContent":["import { deepMerge, type Dict } from '@vitral/core';\nimport { severityDark, severityLight } from '../presets/base/semantic';\nimport { palette, type Palette } from './palette';\nimport type { Preset } from './types';\n\n/** A preset built on another: later arguments override earlier ones, key by key. */\nexport function definePreset(base: Preset, ...overrides: Preset[]): Preset {\n return deepMerge<Preset & Dict>(base as Dict, ...(overrides as Dict[]));\n}\n\n/**\n * Replaces the primary palette, with a colour, a `{palette}` reference or\n * eleven explicit shades, and re-derives the scheme's primary fills from it.\n * The second half matters: a preset such as Avalonia pins `primary.color` to an\n * exact accent, and swapping only the palette would leave that pin in place.\n */\nexport function updatePrimaryPalette(preset: Preset, primary: string | Palette): Preset {\n return definePreset(preset, {\n semantic: {\n primary: typeof primary === 'string' ? palette(primary) : primary,\n colorScheme: { light: { primary: severityLight('primary') }, dark: { primary: severityDark('primary') } }\n }\n });\n}\n\n/** Replaces the neutral surface palette for either scheme, or both. */\nexport function updateSurfacePalette(preset: Preset, surface: { light?: string | Palette; dark?: string | Palette }): Preset {\n const resolve = (value: string | Palette | undefined) => (value === undefined ? undefined : typeof value === 'string' ? palette(value) : value);\n return definePreset(preset, {\n semantic: {\n colorScheme: {\n light: surface.light ? { surface: resolve(surface.light) } : undefined,\n dark: surface.dark ? { surface: resolve(surface.dark) } : undefined\n }\n }\n });\n}\n"],"mappings":";;;;;AAMA,SAAgB,aAAa,MAAc,GAAG,WAA6B;CACvE,OAAO,UAAyB,MAAc,GAAI,SAAoB;AAC1E;;;;;;;AAQA,SAAgB,qBAAqB,QAAgB,SAAmC;CACpF,OAAO,aAAa,QAAQ,EACxB,UAAU;EACN,SAAS,OAAO,YAAY,WAAW,QAAQ,OAAO,IAAI;EAC1D,aAAa;GAAE,OAAO,EAAE,SAAS,cAAc,SAAS,EAAE;GAAG,MAAM,EAAE,SAAS,aAAa,SAAS,EAAE;EAAE;CAC5G,EACJ,CAAC;AACL;;AAGA,SAAgB,qBAAqB,QAAgB,SAAwE;CACzH,MAAM,WAAW,UAAyC,UAAU,KAAA,IAAY,KAAA,IAAY,OAAO,UAAU,WAAW,QAAQ,KAAK,IAAI;CACzI,OAAO,aAAa,QAAQ,EACxB,UAAU,EACN,aAAa;EACT,OAAO,QAAQ,QAAQ,EAAE,SAAS,QAAQ,QAAQ,KAAK,EAAE,IAAI,KAAA;EAC7D,MAAM,QAAQ,OAAO,EAAE,SAAS,QAAQ,QAAQ,IAAI,EAAE,IAAI,KAAA;CAC9D,EACJ,EACJ,CAAC;AACL"}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/** The requested variant: light, dark, or whatever the system says. */
|
|
2
|
+
export type ColorScheme = 'light' | 'dark' | 'system';
|
|
3
|
+
/** A dark mode selector taken apart: a class, an attribute, or neither. */
|
|
4
|
+
export type DarkModeTarget = {
|
|
5
|
+
kind: 'class';
|
|
6
|
+
name: string;
|
|
7
|
+
} | {
|
|
8
|
+
kind: 'attribute';
|
|
9
|
+
name: string;
|
|
10
|
+
value: string;
|
|
11
|
+
} | null;
|
|
12
|
+
/**
|
|
13
|
+
* What `darkModeSelector` asks the document for. `'system'` and `false` return
|
|
14
|
+
* null, since the media query does the work and nothing is set on the element.
|
|
15
|
+
*/
|
|
16
|
+
export declare function parseDarkModeSelector(selector?: string | false | undefined): DarkModeTarget;
|
|
17
|
+
/** Puts the dark scheme on an element, or takes it off. */
|
|
18
|
+
export declare function applyDarkModeTo(root: Element, dark: boolean, selector?: string | false): void;
|
|
19
|
+
/**
|
|
20
|
+
* The attributes `<html>` needs for this scheme, for a server that already
|
|
21
|
+
* knows which one to send (from a cookie, a session, the user's account).
|
|
22
|
+
* Spread them onto the element: `<html {...colorSchemeAttrs(dark)}>`.
|
|
23
|
+
*/
|
|
24
|
+
export declare function colorSchemeAttrs(dark: boolean, selector?: string | false): Record<string, string>;
|
|
25
|
+
export interface ColorSchemeScriptOptions {
|
|
26
|
+
/** The localStorage key the theme manager was given, if it remembers the choice at all. */
|
|
27
|
+
storageKey?: string | false;
|
|
28
|
+
darkModeSelector?: string | false;
|
|
29
|
+
/** The scheme to fall back on when nothing is stored. Defaults to `'system'`. */
|
|
30
|
+
colorScheme?: ColorScheme;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* A script for the `<head>`, above the content. It reads the remembered scheme
|
|
34
|
+
* and the system preference and marks `<html>` before the first paint, so a
|
|
35
|
+
* dark page does not flash white while the app loads.
|
|
36
|
+
*
|
|
37
|
+
* Only the body of the script: the caller wraps it in a `<script>` and, under a
|
|
38
|
+
* strict Content-Security-Policy, puts the nonce on that element.
|
|
39
|
+
*/
|
|
40
|
+
export declare function colorSchemeScript(options?: ColorSchemeScriptOptions): string;
|