lostcoords-ui 0.8.1 → 0.9.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.
Files changed (35) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +124 -66
  3. data/app/assets/tailwind/lostcoords/ui/components/shell.css +4 -0
  4. data/app/assets/tailwind/lostcoords/ui/components.css +1 -0
  5. data/app/assets/tailwind/lostcoords/ui/core.css +1 -1
  6. data/app/assets/tailwind/lostcoords/ui/dialog.css +3 -0
  7. data/app/assets/tailwind/lostcoords/ui/prose.css +9 -0
  8. data/app/assets/tailwind/lostcoords/ui/shell.css +161 -0
  9. data/app/assets/tailwind/lostcoords/ui/typography.css +26 -0
  10. data/app/helpers/lostcoords/ui/emoji_picker_helper.rb +8 -5
  11. data/app/helpers/lostcoords/ui/layout_helper.rb +5 -3
  12. data/app/helpers/lostcoords/ui/navigation_helper.rb +29 -0
  13. data/app/helpers/lostcoords/ui/reading_helper.rb +23 -2
  14. data/app/helpers/lostcoords/ui/shell_helper.rb +64 -0
  15. data/app/helpers/lostcoords/ui/translation_helper.rb +1 -5
  16. data/app/helpers/lostcoords/ui/typography_helper.rb +9 -2
  17. data/app/javascript/lostcoords/ui/controllers/lc_app_header_controller.js +15 -0
  18. data/app/javascript/lostcoords/ui/controllers/lc_dock_controller.js +21 -0
  19. data/app/javascript/lostcoords/ui/controllers/lc_nav_controller.js +13 -7
  20. data/app/javascript/lostcoords/ui/controllers/lc_scroll_keeper_controller.js +53 -0
  21. data/app/javascript/lostcoords/ui/controllers/lc_toc_controller.js +96 -0
  22. data/app/javascript/lostcoords/ui/index.js +8 -0
  23. data/config/locales/en.yml +9 -0
  24. data/config/locales/es.yml +9 -0
  25. data/config/locales/ja.yml +9 -0
  26. data/lib/lostcoords/ui/helpers.rb +1 -1
  27. data/lib/lostcoords/ui/icons.rb +1 -1
  28. data/lib/lostcoords/ui/parts.rb +87 -0
  29. data/lib/lostcoords/ui/version.rb +1 -1
  30. data/lib/lostcoords/ui.rb +12 -2
  31. data/lib/tasks/lostcoords_ui.rake +0 -1
  32. metadata +10 -6
  33. data/lib/lostcoords/ui/action_view.rb +0 -13
  34. data/lib/lostcoords/ui/rails.rb +0 -3
  35. data/lib/lostcoords/ui/resources.rb +0 -17
@@ -0,0 +1,64 @@
1
+ module Lostcoords::Ui
2
+ # Page chrome: the sticky application header, the documentation layout and the phone dock.
3
+ module ShellHelper
4
+ DOCS_SHELL = "lc-docs-shell mx-auto flex w-full max-w-360 gap-10 px-5 sm:px-8 lg:px-10"
5
+ DOCS_SHELL_ASIDE = "sticky top-(--lc-sticky-offset,0rem) hidden h-[calc(100dvh-var(--lc-sticky-offset,0rem))] shrink-0 overflow-y-auto"
6
+
7
+ # A sidebar, the page and an optional "On this page" column. navigation: is the sidebar, usually
8
+ # lc_nav_list; it also fills a collapsible menu above the page below lg. contents: is usually lc_toc
9
+ # and shows from xl. main: adds attributes to the <main>, such as a max width for reading pages.
10
+ def lc_docs_shell(navigation:, contents: nil, menu_label: lc_t("lostcoords.ui.docs_shell.menu"), sidebar_label: lc_t("lostcoords.ui.docs_shell.sidebar"), main: {}, **attributes, &block)
11
+ lc_label!(menu_label, :menu_label)
12
+ lc_label!(sidebar_label, :sidebar_label)
13
+ raise ArgumentError, "lc_docs_shell needs navigation:" if navigation.blank?
14
+
15
+ chevron = lc_icon("chevron-down", options: { class: "size-4 transition-transform duration-(--lc-motion-quick) group-open/menu:rotate-180", "aria-hidden": true })
16
+ menu = tag.details(class: "group/menu relative z-raised rounded-2xl lc-squircle bg-muted lg:hidden", data: { slot: "docs-shell-menu" }) do
17
+ safe_join([
18
+ tag.summary(safe_join([ menu_label, chevron ]), class: "flex min-h-12 cursor-pointer list-none items-center justify-between gap-content px-4 text-label [&::-webkit-details-marker]:hidden"),
19
+ tag.div(navigation, class: "max-h-[60dvh] overflow-y-auto p-3 pt-0")
20
+ ])
21
+ end
22
+ page = tag.main(**lc_attributes({ id: "main", class: "isolate min-w-0 flex-1 py-5 lg:py-10", data: { slot: "docs-shell-main" } }, main)) do
23
+ safe_join([ menu, tag.div(capture(&block), class: "pt-6 lg:pt-0") ])
24
+ end
25
+
26
+ tag.div(**lc_attributes({ class: DOCS_SHELL, data: { slot: "docs-shell" } }, attributes)) do
27
+ safe_join([
28
+ tag.aside(navigation, tabindex: 0, aria: { label: sidebar_label }, class: lc_tw(DOCS_SHELL_ASIDE, "lc-docs-shell__sidebar w-52 py-8 pe-4 lg:block"),
29
+ data: { slot: "docs-shell-sidebar", controller: "lc-scroll-keeper", lc_scroll_keeper_key_value: "lc-docs-shell" }),
30
+ page,
31
+ (tag.aside(contents, class: lc_tw(DOCS_SHELL_ASIDE, "w-44 py-12 ps-4 xl:block"), data: { slot: "docs-shell-contents" }) if contents.present?)
32
+ ].compact)
33
+ end
34
+ end
35
+
36
+ # A sticky header whose glass fades in once the page scrolls. Yields a builder: header.brand,
37
+ # header.nav and header.actions render in that order along the bar. bar: adds attributes to the
38
+ # bar, such as a max width. While it renders, --lc-sticky-offset clears it for anchors and sidebars.
39
+ def lc_app_header(bar: {}, **attributes, &block)
40
+ raise ArgumentError, "lc_app_header needs a block" unless block
41
+
42
+ content = capture(Parts::AppHeader.new(self), &block)
43
+ tag.header(**lc_attributes({ class: "lc-app-header sticky top-0 z-sticky px-3 pt-3 pb-4 sm:px-6",
44
+ data: { slot: "app-header", controller: "lc-app-header", action: "scroll@window->lc-app-header#scroll" } }, attributes)) do
45
+ tag.div(**lc_attributes({ class: "relative isolate mx-auto flex min-h-16 w-full max-w-page items-center gap-related px-3 py-2 sm:gap-content sm:px-5", data: { slot: "app-header-bar" } }, bar)) do
46
+ safe_join([ tag.span(class: lc_glass_class(thick: true, extra: "lc-app-header__glass pointer-events-none absolute inset-0 -z-(--lc-z-raised)"), aria: { hidden: true }), content ])
47
+ end
48
+ end
49
+ end
50
+
51
+ # A floating bar at the bottom of a phone screen: dock.nav for primary destinations and dock.menu
52
+ # for a button whose actions fan out above it. It reserves its height at the end of the page, so
53
+ # render it last, and it hides while the on-screen keyboard is open.
54
+ def lc_dock(**attributes, &block)
55
+ raise ArgumentError, "lc_dock needs a block" unless block
56
+
57
+ content = capture(Parts::Dock.new(self), &block)
58
+ tag.div(**lc_attributes({ class: "lc-dock", data: { slot: "dock", controller: "lc-dock",
59
+ action: "resize@window->lc-dock#keyboard focusin@document->lc-dock#keyboard focusout@document->lc-dock#keyboard" } }, attributes)) do
60
+ safe_join([ content, tag.div(class: "lc-dock__space", aria: { hidden: true }) ])
61
+ end
62
+ end
63
+ end
64
+ end
@@ -3,11 +3,7 @@ module Lostcoords::Ui
3
3
  private
4
4
 
5
5
  def lc_t(key, **options)
6
- I18n.translate(key, **options, locale: lc_locale)
7
- end
8
-
9
- def lc_locale
10
- I18n.locale
6
+ I18n.translate(key, **options)
11
7
  end
12
8
  end
13
9
  end
@@ -24,10 +24,17 @@ module Lostcoords::Ui
24
24
  lc_tw(KBD_BASE, extra)
25
25
  end
26
26
 
27
- def lc_heading(content = nil, level: 2, size: :heading, trim: false, **attributes, &block)
27
+ # anchor: true links the heading to itself, so readers can copy a link to it; it needs id:.
28
+ # anchor: "section-id" links it to an element around it instead, such as its <section>.
29
+ def lc_heading(content = nil, level: 2, size: :heading, trim: false, anchor: false, **attributes, &block)
28
30
  raise ArgumentError, "Invalid level: #{level.inspect}. Expected an integer from 1 to 6" unless (1..6).include?(level) && level.is_a?(Integer)
29
31
 
30
- tag.public_send("h#{level}", content, **lc_attributes({ class: lc_heading_class(size: size, trim: trim) }, attributes), &block)
32
+ target = anchor == true ? attributes[:id].to_s : anchor.presence&.to_s
33
+ raise ArgumentError, "anchor: true needs an id" if anchor == true && target.blank?
34
+
35
+ content = capture(&block) if block
36
+ content = tag.a(content, href: "##{target}", class: "lc-anchor", data: { slot: "heading-anchor" }) if target
37
+ tag.public_send("h#{level}", content, **lc_attributes({ class: lc_heading_class(size: size, trim: trim, extra: ("lc-anchor-target" if anchor == true)) }, attributes))
31
38
  end
32
39
 
33
40
  # as: picks the element, such as :span inside a row or :h3 for a heading set in a text role.
@@ -0,0 +1,15 @@
1
+ import { Controller } from "@hotwired/stimulus"
2
+
3
+ // Fades the header's glass in once the page scrolls.
4
+ export default class extends Controller {
5
+ connect() {
6
+ this.scroll()
7
+ }
8
+
9
+ // An open modal pins the body, so scrollY reads 0 until it closes; the header keeps the
10
+ // state it had rather than fading the glass out behind the dialog.
11
+ scroll() {
12
+ if (document.body.dataset.modalScrollLock === "true") return
13
+ this.element.toggleAttribute("data-scrolled", window.scrollY > 8)
14
+ }
15
+ }
@@ -0,0 +1,21 @@
1
+ import { Controller } from "@hotwired/stimulus"
2
+
3
+ // Hides the dock while the on-screen keyboard is open, so it doesn't cover the field being typed in.
4
+ export default class extends Controller {
5
+ connect() {
6
+ this.viewportHandler = () => this.keyboard()
7
+ window.visualViewport?.addEventListener("resize", this.viewportHandler)
8
+ }
9
+
10
+ disconnect() {
11
+ window.visualViewport?.removeEventListener("resize", this.viewportHandler)
12
+ }
13
+
14
+ keyboard() {
15
+ const typing = document.activeElement?.matches(
16
+ "input, textarea, select, [contenteditable]:not([contenteditable=false])",
17
+ )
18
+ const inset = window.innerHeight - (window.visualViewport?.height ?? window.innerHeight)
19
+ this.element.toggleAttribute("data-keyboard", Boolean(typing) && inset > 150)
20
+ }
21
+ }
@@ -9,9 +9,12 @@ export default class extends Controller {
9
9
 
10
10
  connect() {
11
11
  this.key = this.element.getAttribute("aria-label")
12
- this.initialLink = this.currentLink
13
- this.restore = () => this.select({ currentTarget: this.initialLink }, { animate: false })
14
- document.addEventListener("turbo:before-cache", this.restore)
12
+ // The link the server marked keeps a data attribute, so a page restored from Turbo's cache
13
+ // marks it again instead of the link clicked before leaving. A click on a link to the same
14
+ // page, such as an anchor, keeps the clicked link current.
15
+ const initial = this.linkTargets.find((link) => link.hasAttribute("data-lc-nav-initial"))
16
+ if (initial) this.mark(initial)
17
+ else this.currentLink?.setAttribute("data-lc-nav-initial", "")
15
18
 
16
19
  // The observer reports once on observe, after the first placement below.
17
20
  let observed = false
@@ -29,17 +32,20 @@ export default class extends Controller {
29
32
  disconnect() {
30
33
  cancelAnimationFrame(this.frame)
31
34
  this.observer.disconnect()
32
- document.removeEventListener("turbo:before-cache", this.restore)
33
35
  }
34
36
 
35
- select(event, { animate = true } = {}) {
37
+ select(event) {
36
38
  if (event.button > 0 || event.metaKey || event.ctrlKey || event.shiftKey || event.altKey) return
37
39
 
40
+ this.mark(event.currentTarget)
41
+ this.place({ animate: true })
42
+ }
43
+
44
+ mark(current) {
38
45
  for (const link of this.linkTargets) {
39
- if (link === event.currentTarget) link.setAttribute("aria-current", "page")
46
+ if (link === current) link.setAttribute("aria-current", "page")
40
47
  else link.removeAttribute("aria-current")
41
48
  }
42
- this.place({ animate })
43
49
  }
44
50
 
45
51
  get currentLink() {
@@ -0,0 +1,53 @@
1
+ import { Controller } from "@hotwired/stimulus"
2
+
3
+ // Keeps a scroll container's position across Turbo visits and reloads, such as
4
+ // a sidebar that every page renders again. Positions live in sessionStorage
5
+ // under the key value, so containers that share a key share a position.
6
+ // Without a saved position, the current link scrolls into view instead.
7
+ export default class extends Controller {
8
+ static values = { key: String }
9
+
10
+ connect() {
11
+ this.save = () => this.store()
12
+ document.addEventListener("turbo:before-render", this.save)
13
+ window.addEventListener("pagehide", this.save)
14
+
15
+ const saved = Number.parseFloat(this.read())
16
+ if (Number.isFinite(saved)) this.element.scrollTop = saved
17
+ else this.reveal(this.element.querySelector('[aria-current="page"]'))
18
+ }
19
+
20
+ disconnect() {
21
+ document.removeEventListener("turbo:before-render", this.save)
22
+ window.removeEventListener("pagehide", this.save)
23
+ }
24
+
25
+ // scrollIntoView would also scroll the page, so only the container moves.
26
+ reveal(link) {
27
+ if (!link) return
28
+ const frame = this.element.getBoundingClientRect()
29
+ const box = link.getBoundingClientRect()
30
+ if (box.top >= frame.top && box.bottom <= frame.bottom) return
31
+ this.element.scrollTop += box.top - frame.top - (frame.height - box.height) / 2
32
+ }
33
+
34
+ get storageKey() {
35
+ return `lostcoords-ui:scroll:${this.keyValue || this.element.id || "default"}`
36
+ }
37
+
38
+ read() {
39
+ try {
40
+ return sessionStorage.getItem(this.storageKey)
41
+ } catch {
42
+ return null
43
+ }
44
+ }
45
+
46
+ store() {
47
+ // A hidden container reports 0 and would overwrite the visible one's position.
48
+ if (!this.element.offsetParent) return
49
+ try {
50
+ sessionStorage.setItem(this.storageKey, String(this.element.scrollTop))
51
+ } catch {}
52
+ }
53
+ }
@@ -0,0 +1,96 @@
1
+ import { Controller } from "@hotwired/stimulus"
2
+
3
+ // Marks the link to the section the reader has scrolled to with aria-current="location" and slides
4
+ // the indicator to it. A section is current once its top passes its own scroll margin, which is
5
+ // where a click on its link puts it.
6
+ export default class extends Controller {
7
+ static targets = ["link", "indicator"]
8
+
9
+ connect() {
10
+ this.schedule = () => {
11
+ this.frame ||= requestAnimationFrame(() => {
12
+ this.frame = null
13
+ this.update()
14
+ })
15
+ }
16
+ addEventListener("scroll", this.schedule, { passive: true })
17
+ addEventListener("resize", this.schedule, { passive: true })
18
+ this.update({ animate: false })
19
+ }
20
+
21
+ disconnect() {
22
+ removeEventListener("scroll", this.schedule)
23
+ removeEventListener("resize", this.schedule)
24
+ cancelAnimationFrame(this.frame)
25
+ this.frame = null
26
+ }
27
+
28
+ // The last sections can't scroll up to the top, so at the end of the page the clicked one stays current.
29
+ select(event) {
30
+ this.chosen = event.currentTarget
31
+ }
32
+
33
+ update({ animate = true } = {}) {
34
+ const sections = this.linkTargets.flatMap((link) => {
35
+ const target = document.getElementById(decodeURIComponent(link.hash.slice(1)))
36
+ return target ? [{ link, target, top: target.getBoundingClientRect().top }] : []
37
+ })
38
+ let current = null
39
+ for (const section of sections) {
40
+ const margin = Number.parseFloat(getComputedStyle(section.target).scrollMarginTop) || 0
41
+ if (section.top <= margin + 4) current = section.link
42
+ }
43
+
44
+ const root = document.scrollingElement ?? document.documentElement
45
+ if (root.scrollTop > 0 && root.scrollTop + innerHeight >= root.scrollHeight - 2) {
46
+ const visible = sections.filter((section) => section.top < innerHeight)
47
+ const chosen = visible.find((section) => section.link === this.chosen)
48
+ current = (chosen ?? visible.at(-1))?.link ?? current
49
+ }
50
+ if (current === this.current && !animate) return this.place(current, { animate })
51
+ if (current === this.current) return
52
+
53
+ this.current = current
54
+ for (const link of this.linkTargets) {
55
+ if (link === current) link.setAttribute("aria-current", "location")
56
+ else link.removeAttribute("aria-current")
57
+ }
58
+ this.place(current, { animate })
59
+ this.reveal(current)
60
+ }
61
+
62
+ place(link, { animate }) {
63
+ if (!this.hasIndicatorTarget) return
64
+
65
+ const style = this.indicatorTarget.style
66
+ style.transition = animate ? "" : "none"
67
+ if (link) {
68
+ style.translate = `0 ${link.offsetTop}px`
69
+ style.height = `${link.offsetHeight}px`
70
+ }
71
+ style.opacity = link ? "1" : "0"
72
+ if (!animate) {
73
+ this.indicatorTarget.offsetWidth
74
+ style.transition = ""
75
+ }
76
+ }
77
+
78
+ // Keeps the current link inside a scrolling sidebar without scrolling the page.
79
+ reveal(link) {
80
+ if (!link) return
81
+
82
+ let box = this.element.parentElement
83
+ while (box && box !== document.body) {
84
+ const { overflowY } = getComputedStyle(box)
85
+ if ((overflowY === "auto" || overflowY === "scroll") && box.scrollHeight > box.clientHeight)
86
+ break
87
+ box = box.parentElement
88
+ }
89
+ if (!box || box === document.body) return
90
+
91
+ const linkBox = link.getBoundingClientRect()
92
+ const frame = box.getBoundingClientRect()
93
+ if (linkBox.top < frame.top) box.scrollTop -= frame.top - linkBox.top
94
+ else if (linkBox.bottom > frame.bottom) box.scrollTop += linkBox.bottom - frame.bottom
95
+ }
96
+ }
@@ -1,12 +1,15 @@
1
+ import AppHeader from "lostcoords/ui/controllers/lc_app_header_controller"
1
2
  import CodeBlock from "lostcoords/ui/controllers/lc_code_block_controller"
2
3
  import Collapsible from "lostcoords/ui/controllers/lc_collapsible_controller"
3
4
  import Copy from "lostcoords/ui/controllers/lc_copy_controller"
4
5
  import Dialog from "lostcoords/ui/controllers/lc_dialog_controller"
6
+ import Dock from "lostcoords/ui/controllers/lc_dock_controller"
5
7
  import EmojiPicker from "lostcoords/ui/controllers/lc_emoji_picker_controller"
6
8
  import Kbd from "lostcoords/ui/controllers/lc_kbd_controller"
7
9
  import Menu from "lostcoords/ui/controllers/lc_menu_controller"
8
10
  import Nav from "lostcoords/ui/controllers/lc_nav_controller"
9
11
  import Popover from "lostcoords/ui/controllers/lc_popover_controller"
12
+ import ScrollKeeper from "lostcoords/ui/controllers/lc_scroll_keeper_controller"
10
13
  import Search from "lostcoords/ui/controllers/lc_search_controller"
11
14
  import Selection from "lostcoords/ui/controllers/lc_selection_controller"
12
15
  import Sheet from "lostcoords/ui/controllers/lc_sheet_controller"
@@ -14,24 +17,29 @@ import Shortcut from "lostcoords/ui/controllers/lc_shortcut_controller"
14
17
  import Tabs from "lostcoords/ui/controllers/lc_tabs_controller"
15
18
  import Toast from "lostcoords/ui/controllers/lc_toast_controller"
16
19
  import ToastRegion from "lostcoords/ui/controllers/lc_toast_region_controller"
20
+ import Toc from "lostcoords/ui/controllers/lc_toc_controller"
17
21
  import Toggle from "lostcoords/ui/controllers/lc_toggle_controller"
18
22
  import Tooltip from "lostcoords/ui/controllers/lc_tooltip_controller"
19
23
 
20
24
  const controllers = {
25
+ "lc-app-header": AppHeader,
21
26
  "lc-collapsible": Collapsible,
22
27
  "lc-code-block": CodeBlock,
23
28
  "lc-copy": Copy,
24
29
  "lc-dialog": Dialog,
30
+ "lc-dock": Dock,
25
31
  "lc-emoji-picker": EmojiPicker,
26
32
  "lc-kbd": Kbd,
27
33
  "lc-menu": Menu,
28
34
  "lc-nav": Nav,
29
35
  "lc-popover": Popover,
36
+ "lc-scroll-keeper": ScrollKeeper,
30
37
  "lc-search": Search,
31
38
  "lc-selection": Selection,
32
39
  "lc-sheet": Sheet,
33
40
  "lc-shortcut": Shortcut,
34
41
  "lc-tabs": Tabs,
42
+ "lc-toc": Toc,
35
43
  "lc-toast": Toast,
36
44
  "lc-toast-region": ToastRegion,
37
45
  "lc-toggle": Toggle,
@@ -20,6 +20,15 @@ en:
20
20
  label: "Actions"
21
21
  popover:
22
22
  trigger: "Open popover"
23
+ toc:
24
+ label: "On this page"
25
+ docs_shell:
26
+ menu: "Menu"
27
+ sidebar: "Documentation sidebar"
28
+ app_header:
29
+ navigation: "Main"
30
+ dock:
31
+ close: "Close %{label}"
23
32
  breadcrumb:
24
33
  label: "Breadcrumb"
25
34
  code_block:
@@ -20,6 +20,15 @@ es:
20
20
  label: "Acciones"
21
21
  popover:
22
22
  trigger: "Abrir panel"
23
+ toc:
24
+ label: "En esta página"
25
+ docs_shell:
26
+ menu: "Menú"
27
+ sidebar: "Barra lateral de la documentación"
28
+ app_header:
29
+ navigation: "Principal"
30
+ dock:
31
+ close: "Cerrar %{label}"
23
32
  breadcrumb:
24
33
  label: "Ruta de navegación"
25
34
  code_block:
@@ -20,6 +20,15 @@ ja:
20
20
  label: "操作"
21
21
  popover:
22
22
  trigger: "パネルを開く"
23
+ toc:
24
+ label: "このページの内容"
25
+ docs_shell:
26
+ menu: "メニュー"
27
+ sidebar: "ドキュメントのサイドバー"
28
+ app_header:
29
+ navigation: "メイン"
30
+ dock:
31
+ close: "%{label}を閉じる"
23
32
  breadcrumb:
24
33
  label: "パンくずリスト"
25
34
  code_block:
@@ -1,6 +1,6 @@
1
1
  module Lostcoords::Ui
2
2
  module Helpers
3
- paths = Dir[Lostcoords::Ui.root.join("app/helpers/lostcoords/ui/*_helper.rb")].sort_by do |path|
3
+ paths = Dir[Lostcoords::Ui::Engine.root.join("app/helpers/lostcoords/ui/*_helper.rb")].sort_by do |path|
4
4
  [ %w[tailwind_helper.rb button_helper.rb].index(File.basename(path)) || 2, path ]
5
5
  end
6
6
  paths.each do |path|
@@ -8,7 +8,7 @@ module Lostcoords::Ui
8
8
 
9
9
  def self.fetch(name, variant)
10
10
  LOCK.synchronize do
11
- @catalog ||= JSON.parse(File.read(Lostcoords::Ui.root.join("vendor/heroicons/icons.json")), freeze: true).fetch("icons")
11
+ @catalog ||= JSON.parse(File.read(Lostcoords::Ui::Engine.root.join("vendor/heroicons/icons.json")), freeze: true).fetch("icons")
12
12
  source = @catalog.fetch(variant.to_s).fetch(name.to_s)
13
13
  template = TEMPLATES[source] ||= begin
14
14
  document = Nokogiri::XML(source) { |config| config.strict.nonet }
@@ -195,6 +195,93 @@ module Lostcoords::Ui
195
195
  end
196
196
  end
197
197
 
198
+ class Toc < Base
199
+ LINK = "relative block py-tight ps-content pe-tight text-small text-muted-foreground no-underline transition-colors " \
200
+ "duration-(--lc-motion-quick) hover:text-foreground aria-[current=location]:font-medium aria-[current=location]:text-foreground"
201
+
202
+ # url: an in-page anchor such as "#usage". indent: true nests the entry under the one above it.
203
+ def link(label, url, indent: false, **attributes)
204
+ view.send(:lc_label!, label, :label)
205
+ raise ArgumentError, "toc link needs an anchor URL, such as \"#usage\"" unless url.to_s.match?(/#./)
206
+
207
+ view.tag.li(data: { slot: "toc-item" }) do
208
+ view.link_to(label, url, **merge({ class: view.lc_tw(LINK, ("ps-[calc(var(--spacing-content)*2)]" if indent)),
209
+ data: { slot: "toc-link", lc_toc_target: "link", action: "lc-toc#select" } }, attributes))
210
+ end
211
+ end
212
+ end
213
+
214
+ class AppHeader < Base
215
+ # The home link. label: names it for screen readers, since the block is usually a logo.
216
+ def brand(url = "/", label:, **attributes, &block)
217
+ view.send(:lc_label!, label, :label)
218
+ raise ArgumentError, "header brand needs a block" unless block
219
+
220
+ view.link_to(url, **merge({ class: "flex min-h-11 shrink-0 items-center gap-related pe-2 font-semibold tracking-tight text-inherit no-underline",
221
+ aria: { label: label }, data: { slot: "app-header-brand" } }, attributes), &block)
222
+ end
223
+
224
+ # A row of links with a sliding indicator; yields the lc_nav_list builder. Hide it on phones
225
+ # with class: "max-sm:hidden" when a dock or menu carries the same links.
226
+ def nav(label: view.send(:lc_t, "lostcoords.ui.app_header.navigation"), **attributes, &block)
227
+ view.lc_nav_list(label: label, layout: :row, indicator: true, **attributes, &block)
228
+ end
229
+
230
+ # Controls at the end of the bar, such as search, a theme switch or an account menu.
231
+ def actions(**attributes, &block)
232
+ part(:div, "app-header-actions", "ms-auto flex shrink-0 items-center gap-related", attributes, &block)
233
+ end
234
+ end
235
+
236
+ class Dock < Base
237
+ # The primary destinations, icon above label; yields the lc_nav_list builder.
238
+ def nav(label:, **attributes, &block)
239
+ view.lc_nav_list(label: label, layout: :dock, indicator: true,
240
+ **merge({ class: view.lc_glass_class(shape: :panel, thick: true, extra: "lc-dock__nav rounded-pill") }, attributes), &block)
241
+ end
242
+
243
+ # A round button at the end of the dock whose actions fan out above it. Yields a builder:
244
+ # `menu.action "New note", icon: "pencil-square"`.
245
+ def menu(label:, icon: "plus", id: nil, **attributes, &block)
246
+ view.send(:lc_label!, label, :label)
247
+ raise ArgumentError, "dock menu needs a block" unless block
248
+
249
+ id ||= "#{label.parameterize.presence || 'dock'}-menu"
250
+ actions = view.capture(DockMenu.new(view), &block)
251
+ view.tag.div(**merge({ class: "lc-dock__menu", data: { slot: "dock-menu", controller: "lc-menu", lc_menu_lift_value: false } }, attributes)) do
252
+ view.safe_join([
253
+ view.tag.button(view.lc_icon(icon, options: { class: "size-7", "aria-hidden": true }), type: "button",
254
+ class: view.lc_glass_class(shape: :disc, thick: true, extra: "lc-dock__trigger"), aria: { label: label, haspopup: "menu", expanded: false, controls: id },
255
+ data: { slot: "dock-menu-trigger", lc_menu_target: "trigger", action: "lc-menu#toggle" }),
256
+ view.tag.div(id: id, class: "lc-dock__actions", role: "menu", aria: { label: label }, data: { lc_menu_target: "popover" }) do
257
+ view.safe_join([
258
+ view.tag.button(type: "button", class: "lc-dock__scrim", tabindex: -1, aria: { label: view.send(:lc_t, "lostcoords.ui.dock.close", label: label) }, data: { action: "lc-menu#close" }),
259
+ actions
260
+ ])
261
+ end
262
+ ])
263
+ end
264
+ end
265
+ end
266
+
267
+ class DockMenu < Base
268
+ def initialize(view)
269
+ super
270
+ @count = 0
271
+ end
272
+
273
+ # An action that fans out above the dock button. Pass data: { action: "…" } for your controller;
274
+ # the menu closes after it runs.
275
+ def action(label, icon:, **attributes)
276
+ view.send(:lc_label!, label, :label)
277
+ @count += 1
278
+ defaults = { class: view.lc_glass_class(shape: :disc, thick: true, extra: "lc-dock__action"), data: { slot: "dock-action", action: "lc-menu#close" } }
279
+ view.tag.button(**merge(defaults, attributes), type: "button", role: "menuitem", style: "--lc-dock-action-index: #{@count}") do
280
+ view.safe_join([ view.tag.span(label, class: "lc-dock__action-label"), view.lc_icon(icon, options: { class: "size-6", "aria-hidden": true }) ])
281
+ end
282
+ end
283
+ end
284
+
198
285
  class Panel < Base
199
286
  include CustomTrigger
200
287
 
@@ -1,5 +1,5 @@
1
1
  module Lostcoords
2
2
  module Ui
3
- VERSION = "0.8.1"
3
+ VERSION = "0.9.0"
4
4
  end
5
5
  end
data/lib/lostcoords/ui.rb CHANGED
@@ -1,2 +1,12 @@
1
- require "lostcoords/ui/action_view"
2
- require "lostcoords/ui/rails" if defined?(::Rails::Engine)
1
+ require "rails/engine"
2
+ require "action_view"
3
+ require "active_support/core_ext/hash"
4
+ require "active_support/core_ext/object/blank"
5
+ require "tailwind_merge"
6
+ require "lostcoords/ui/version"
7
+ require "lostcoords/ui/engine"
8
+ require "lostcoords/ui/icons"
9
+ require "lostcoords/ui/parts"
10
+ require "lostcoords/ui/form_builder"
11
+ require "lostcoords/ui/helpers"
12
+ require "lostcoords/ui/stylesheets"
@@ -15,7 +15,6 @@ namespace :lostcoords do
15
15
  end
16
16
 
17
17
  # Runs with tailwindcss-rails' own engines step, before every tailwindcss:build and tailwindcss:watch.
18
- # Apps without tailwindcss-rails import the gem's stylesheets by path instead.
19
18
  if defined?(Tailwindcss::Engines)
20
19
  namespace :tailwindcss do
21
20
  task :engines do