dialog-theme-list.tsx 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import { DialogSelect, type DialogSelectRef } from "../ui/dialog-select"
  2. import { THEMES, useTheme } from "../context/theme"
  3. import { useDialog } from "../ui/dialog"
  4. import { onCleanup, onMount } from "solid-js"
  5. export function DialogThemeList() {
  6. const { selectedTheme, setSelectedTheme } = useTheme()
  7. const options = Object.keys(THEMES).map((value) => ({
  8. title: value,
  9. value: value as keyof typeof THEMES,
  10. }))
  11. const initial = selectedTheme()
  12. const dialog = useDialog()
  13. let confirmed = false
  14. let ref: DialogSelectRef<keyof typeof THEMES>
  15. onMount(() => {
  16. // highlight the first theme in the list when we open it for UX
  17. setSelectedTheme(Object.keys(THEMES)[0] as keyof typeof THEMES)
  18. })
  19. onCleanup(() => {
  20. // if we close the dialog without confirming, reset back to the initial theme
  21. if (!confirmed) setSelectedTheme(initial)
  22. })
  23. return (
  24. <DialogSelect
  25. title="Themes"
  26. options={options}
  27. onMove={(opt) => {
  28. setSelectedTheme(opt.value)
  29. }}
  30. onSelect={(opt) => {
  31. setSelectedTheme(opt.value)
  32. confirmed = true
  33. dialog.clear()
  34. }}
  35. ref={(r) => {
  36. ref = r
  37. }}
  38. onFilter={(query) => {
  39. if (query.length === 0) {
  40. setSelectedTheme(initial)
  41. return
  42. }
  43. const first = ref.filtered[0]
  44. if (first) setSelectedTheme(first.value)
  45. }}
  46. />
  47. )
  48. }