lostcoords-ui 0.10.0 → 0.12.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 (33) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +155 -23
  3. data/app/assets/tailwind/lostcoords/ui/base.css +1 -1
  4. data/app/assets/tailwind/lostcoords/ui/components/hovercard.css +4 -0
  5. data/app/assets/tailwind/lostcoords/ui/components.css +1 -0
  6. data/app/assets/tailwind/lostcoords/ui/core.css +2 -2
  7. data/app/assets/tailwind/lostcoords/ui/emoji-picker.css +1 -1
  8. data/app/assets/tailwind/lostcoords/ui/hovercard.css +47 -0
  9. data/app/helpers/lostcoords/ui/avatar_badge_helper.rb +43 -0
  10. data/app/helpers/lostcoords/ui/badge_helper.rb +20 -2
  11. data/app/helpers/lostcoords/ui/button_helper.rb +16 -4
  12. data/app/helpers/lostcoords/ui/card_helper.rb +3 -1
  13. data/app/helpers/lostcoords/ui/choice_helper.rb +1 -2
  14. data/app/helpers/lostcoords/ui/emoji_picker_helper.rb +5 -1
  15. data/app/helpers/lostcoords/ui/file_drop_helper.rb +32 -0
  16. data/app/helpers/lostcoords/ui/form_helper.rb +18 -4
  17. data/app/helpers/lostcoords/ui/hovercard_helper.rb +31 -0
  18. data/app/helpers/lostcoords/ui/list_group_helper.rb +38 -3
  19. data/app/helpers/lostcoords/ui/menu_helper.rb +23 -14
  20. data/app/helpers/lostcoords/ui/primitives_helper.rb +20 -12
  21. data/app/helpers/lostcoords/ui/render_attributes_helper.rb +1 -1
  22. data/app/helpers/lostcoords/ui/sheet_helper.rb +9 -3
  23. data/app/helpers/lostcoords/ui/structures_helper.rb +37 -17
  24. data/app/helpers/lostcoords/ui/typography_helper.rb +4 -2
  25. data/app/javascript/lostcoords/ui/controllers/lc_hovercard_controller.js +104 -0
  26. data/app/javascript/lostcoords/ui/helpers/dialog.js +27 -4
  27. data/app/javascript/lostcoords/ui/helpers/dismiss_stack.js +1 -1
  28. data/app/javascript/lostcoords/ui/index.js +2 -0
  29. data/app/views/lostcoords/ui/components/_lc_dialog.html.erb +2 -1
  30. data/lib/lostcoords/ui/form_builder.rb +7 -0
  31. data/lib/lostcoords/ui/parts.rb +15 -0
  32. data/lib/lostcoords/ui/version.rb +1 -1
  33. metadata +7 -1
@@ -0,0 +1,104 @@
1
+ import { Controller } from "@hotwired/stimulus"
2
+ import { registerDismissable } from "lostcoords/ui/helpers/dismiss_stack"
3
+ import { liftToTopLayer, onPageLeave } from "lostcoords/ui/helpers/top_layer"
4
+
5
+ const OPEN_DELAY = 450
6
+ const CLOSE_DELAY = 200
7
+
8
+ // Opens a preview card after a short hover, or at once on keyboard focus, in
9
+ // the top layer beside its trigger. With a src it loads the card's HTML the
10
+ // first time it opens. The pointer can cross the gap to the card and stay on
11
+ // it; leaving both closes it after a moment, and Escape closes it at once.
12
+ // Touch screens never open it: a tap follows the link.
13
+ export default class extends Controller {
14
+ static targets = ["trigger", "content"]
15
+ static values = { src: String }
16
+
17
+ connect() {
18
+ this.stopPageLeave = onPageLeave(() => this.close({ immediate: true }))
19
+ this.untab()
20
+ }
21
+
22
+ disconnect() {
23
+ this.stopPageLeave?.()
24
+ this.cancelTimers()
25
+ this.close({ immediate: true })
26
+ }
27
+
28
+ enter(event) {
29
+ if (event.type === "focusin" && !event.target.matches(":focus-visible")) return
30
+ if (event.type === "mouseenter" && !matchMedia("(hover: hover)").matches) return
31
+ clearTimeout(this.closing)
32
+ if (this.release || this.opening) return
33
+ this.opening = setTimeout(() => this.open(), event.type === "focusin" ? 0 : OPEN_DELAY)
34
+ }
35
+
36
+ leave() {
37
+ clearTimeout(this.opening)
38
+ this.opening = null
39
+ this.closing = setTimeout(() => {
40
+ if (this.element.matches(":hover") || this.element.querySelector(":focus-visible")) return
41
+ this.close()
42
+ }, CLOSE_DELAY)
43
+ }
44
+
45
+ async open() {
46
+ this.opening = null
47
+ if (this.release || !this.element.isConnected) return
48
+ if (!this.contentTarget.hasAttribute("data-loaded") && this.hasSrcValue && this.srcValue) {
49
+ this.loading ??= this.load().finally(() => (this.loading = null))
50
+ if (!(await this.loading) || this.release) return
51
+ if (!this.element.matches(":hover") && !this.element.querySelector(":focus-visible")) return
52
+ }
53
+ this.element.toggleAttribute("data-open", true)
54
+ this.release = liftToTopLayer(this.contentTarget, this.triggerTarget)
55
+ this.unregister = registerDismissable({
56
+ type: "hovercard",
57
+ element: this.element,
58
+ isOpen: () => this.element.hasAttribute("data-open"),
59
+ close: () => this.close({ immediate: true }),
60
+ })
61
+ }
62
+
63
+ // Only the page's own origin can fill the card.
64
+ async load() {
65
+ const url = new URL(this.srcValue, window.location.href)
66
+ if (url.origin !== window.location.origin) return false
67
+ try {
68
+ const response = await fetch(url, {
69
+ headers: { Accept: "text/html", "X-Requested-With": "XMLHttpRequest" },
70
+ credentials: "same-origin",
71
+ })
72
+ if (!response.ok) return false
73
+ this.contentTarget.innerHTML = await response.text()
74
+ this.untab()
75
+ this.contentTarget.toggleAttribute("data-loaded", true)
76
+ return true
77
+ } catch {
78
+ return false
79
+ }
80
+ }
81
+
82
+ // The card is a preview: its links stay clickable but out of the tab order.
83
+ untab() {
84
+ for (const element of this.contentTarget.querySelectorAll(
85
+ "a, button, input, select, textarea, [tabindex]",
86
+ )) {
87
+ element.setAttribute("tabindex", "-1")
88
+ }
89
+ }
90
+
91
+ close({ immediate = false } = {}) {
92
+ this.element.toggleAttribute("data-open", false)
93
+ this.release?.({ immediate })
94
+ this.release = null
95
+ this.unregister?.()
96
+ this.unregister = null
97
+ }
98
+
99
+ cancelTimers() {
100
+ clearTimeout(this.opening)
101
+ clearTimeout(this.closing)
102
+ this.opening = null
103
+ }
104
+ }
@@ -2,7 +2,15 @@ import { registerDismissable } from "lostcoords/ui/helpers/dismiss_stack"
2
2
 
3
3
  const registrations = new WeakMap()
4
4
 
5
- export function registerDialog(dialog, { close = () => dialog.close(), lockScroll } = {}) {
5
+ // Joins a <dialog> to the dismiss stack: Escape, the backdrop and a swipe call close, which may
6
+ // return false to keep it open, such as while it asks whether to discard changes. priority ranks
7
+ // it against panels open in it (a dialog is 10, a panel 20); restoreFocus runs after a close;
8
+ // lockScroll: false leaves the page scrollable behind it. Calling it again for an open dialog
9
+ // returns its registration; it ends when the dialog closes.
10
+ export function registerDialog(
11
+ dialog,
12
+ { close = () => dialog.close(), lockScroll, priority, restoreFocus } = {},
13
+ ) {
6
14
  const existing = registrations.get(dialog)
7
15
  if (existing) return existing
8
16
 
@@ -10,22 +18,37 @@ export function registerDialog(dialog, { close = () => dialog.close(), lockScrol
10
18
  element: dialog,
11
19
  type: "lc-dialog",
12
20
  lockScroll,
21
+ priority,
22
+ restoreFocus,
13
23
  close,
14
24
  isOpen: () => dialog.open,
15
25
  })
26
+ // A click on the dialog element itself lands on its backdrop, or on its own padding, which
27
+ // is inside its box and must not close it.
16
28
  const closeOnBackdrop = (event) => {
17
- if (event.target === dialog) close(event)
29
+ if (event.target !== dialog) return
30
+ const box = dialog.getBoundingClientRect()
31
+ const inside =
32
+ event.clientX >= box.left &&
33
+ event.clientX <= box.right &&
34
+ event.clientY >= box.top &&
35
+ event.clientY <= box.bottom
36
+ if (!inside) close(event)
37
+ }
38
+ // A dialog closed and shown again in the same task fires close while it is already open again.
39
+ const unregisterOnClose = () => {
40
+ if (!dialog.open) unregister()
18
41
  }
19
42
  const unregister = () => {
20
43
  if (registrations.get(dialog) !== unregister) return
21
44
  registrations.delete(dialog)
22
- dialog.removeEventListener("close", unregister)
45
+ dialog.removeEventListener("close", unregisterOnClose)
23
46
  dialog.removeEventListener("click", closeOnBackdrop)
24
47
  unregisterDismissable()
25
48
  }
26
49
 
27
50
  registrations.set(dialog, unregister)
28
- dialog.addEventListener("close", unregister)
51
+ dialog.addEventListener("close", unregisterOnClose)
29
52
  dialog.addEventListener("click", closeOnBackdrop)
30
53
  return unregister
31
54
  }
@@ -130,7 +130,7 @@ function defaultIsOpen() {
130
130
  }
131
131
 
132
132
  function priorityFor(type) {
133
- if (type === "tooltip") return 40
133
+ if (type === "tooltip" || type === "hovercard") return 40
134
134
  if (type === "popover") return 30
135
135
  if (type === "panel") return 20
136
136
  return 10
@@ -6,6 +6,7 @@ import Copy from "lostcoords/ui/controllers/lc_copy_controller"
6
6
  import Dialog from "lostcoords/ui/controllers/lc_dialog_controller"
7
7
  import Dock from "lostcoords/ui/controllers/lc_dock_controller"
8
8
  import EmojiPicker from "lostcoords/ui/controllers/lc_emoji_picker_controller"
9
+ import Hovercard from "lostcoords/ui/controllers/lc_hovercard_controller"
9
10
  import Kbd from "lostcoords/ui/controllers/lc_kbd_controller"
10
11
  import Menu from "lostcoords/ui/controllers/lc_menu_controller"
11
12
  import Nav from "lostcoords/ui/controllers/lc_nav_controller"
@@ -32,6 +33,7 @@ const controllers = {
32
33
  "lc-dialog": Dialog,
33
34
  "lc-dock": Dock,
34
35
  "lc-emoji-picker": EmojiPicker,
36
+ "lc-hovercard": Hovercard,
35
37
  "lc-kbd": Kbd,
36
38
  "lc-menu": Menu,
37
39
  "lc-nav": Nav,
@@ -11,7 +11,8 @@
11
11
  <button type="button" class="<%= lc_icon_button_class(size: :sm, shape: :circle) %>" aria-label="<%= close_label %>" data-action="<%= close_action %>" data-slot="dialog-dismiss"><%= lc_icon "x-mark", options: { "aria-hidden": true } %></button>
12
12
  </div>
13
13
  </header>
14
- <div class="-mx-inset -mb-inset flex min-h-0 flex-1 flex-col gap-inset px-inset pb-inset sm:overflow-y-auto sm:overscroll-contain" data-slot="dialog-body"><%= content %></div>
14
+ <%# Children keep their height, so a long body scrolls instead of squashing a fixed-height button. %>
15
+ <div class="-mx-inset -mb-inset flex min-h-0 flex-1 flex-col gap-inset px-inset pb-inset *:shrink-0 sm:overflow-y-auto sm:overscroll-contain" data-slot="dialog-body"><%= content %></div>
15
16
  </div>
16
17
  <% end %>
17
18
  <% if trigger %>
@@ -80,6 +80,13 @@ module Lostcoords::Ui
80
80
  choice_label(method, label, hint, "switch", input)
81
81
  end
82
82
 
83
+ # lc_file_drop around this attribute's file_field; options other than the drop zone's go to the input.
84
+ def file_drop(method, title:, hint: nil, button: nil, icon: "arrow-up-tray", **options)
85
+ @template.lc_file_drop(title: title, hint: hint, button: button, icon: icon) do
86
+ file_field(method, options.merge(class: @template.lc_tw("sr-only", options[:class])))
87
+ end
88
+ end
89
+
83
90
  # A checkbox with its label beside it.
84
91
  def choice(method, label: nil, hint: nil, checked_value: "1", unchecked_value: "0", **options)
85
92
  input = rails_check_box(method, control_options(method, options, "checkbox-input", @template.lc_checkbox_class, described: true), checked_value, unchecked_value)
@@ -85,6 +85,21 @@ module Lostcoords::Ui
85
85
  end
86
86
  end
87
87
 
88
+ # Rows that stay put while the sheet's content scrolls under them, painted with the sheet's surface.
89
+ # The footer sits last in an inset-padded scroller and bleeds to its edges, clearing the home indicator.
90
+ class Sheet < Base
91
+ SURFACE = "sticky z-sticky bg-(--lc-sheet-bg,var(--popover))"
92
+
93
+ def header(**attributes, &block)
94
+ part(:header, "sheet-header", "#{SURFACE} top-0 flex min-h-14 shrink-0 items-center", attributes, &block)
95
+ end
96
+
97
+ def footer(**attributes, &block)
98
+ part(:footer, "sheet-footer", "#{SURFACE} bottom-0 -mx-inset flex flex-wrap items-center justify-end gap-related " \
99
+ "px-inset pt-3 pb-[max(env(safe-area-inset-bottom),--spacing(3))]", attributes, &block)
100
+ end
101
+ end
102
+
88
103
  class Dialog < Base
89
104
  include CustomTrigger
90
105
 
@@ -1,5 +1,5 @@
1
1
  module Lostcoords
2
2
  module Ui
3
- VERSION = "0.10.0"
3
+ VERSION = "0.12.0"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lostcoords-ui
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.10.0
4
+ version: 0.12.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lost Coordinates
@@ -96,6 +96,7 @@ files:
96
96
  - app/assets/tailwind/lostcoords/ui/components/collapsible.css
97
97
  - app/assets/tailwind/lostcoords/ui/components/dialog.css
98
98
  - app/assets/tailwind/lostcoords/ui/components/emoji-picker.css
99
+ - app/assets/tailwind/lostcoords/ui/components/hovercard.css
99
100
  - app/assets/tailwind/lostcoords/ui/components/menu.css
100
101
  - app/assets/tailwind/lostcoords/ui/components/prose.css
101
102
  - app/assets/tailwind/lostcoords/ui/components/search.css
@@ -109,6 +110,7 @@ files:
109
110
  - app/assets/tailwind/lostcoords/ui/emoji-picker.css
110
111
  - app/assets/tailwind/lostcoords/ui/field.css
111
112
  - app/assets/tailwind/lostcoords/ui/glass.css
113
+ - app/assets/tailwind/lostcoords/ui/hovercard.css
112
114
  - app/assets/tailwind/lostcoords/ui/layout.css
113
115
  - app/assets/tailwind/lostcoords/ui/popover.css
114
116
  - app/assets/tailwind/lostcoords/ui/prose.css
@@ -125,6 +127,7 @@ files:
125
127
  - app/assets/tailwind/lostcoords/ui/ui.css
126
128
  - app/assets/tailwind/lostcoords/ui/utilities.css
127
129
  - app/assets/tailwind/lostcoords_ui/engine.css
130
+ - app/helpers/lostcoords/ui/avatar_badge_helper.rb
128
131
  - app/helpers/lostcoords/ui/badge_helper.rb
129
132
  - app/helpers/lostcoords/ui/button_helper.rb
130
133
  - app/helpers/lostcoords/ui/card_helper.rb
@@ -136,8 +139,10 @@ files:
136
139
  - app/helpers/lostcoords/ui/dialog_helper.rb
137
140
  - app/helpers/lostcoords/ui/emoji_picker_helper.rb
138
141
  - app/helpers/lostcoords/ui/feedback_helper.rb
142
+ - app/helpers/lostcoords/ui/file_drop_helper.rb
139
143
  - app/helpers/lostcoords/ui/form_helper.rb
140
144
  - app/helpers/lostcoords/ui/glass_helper.rb
145
+ - app/helpers/lostcoords/ui/hovercard_helper.rb
141
146
  - app/helpers/lostcoords/ui/icon_helper.rb
142
147
  - app/helpers/lostcoords/ui/layout_helper.rb
143
148
  - app/helpers/lostcoords/ui/list_group_helper.rb
@@ -164,6 +169,7 @@ files:
164
169
  - app/javascript/lostcoords/ui/controllers/lc_dialog_controller.js
165
170
  - app/javascript/lostcoords/ui/controllers/lc_dock_controller.js
166
171
  - app/javascript/lostcoords/ui/controllers/lc_emoji_picker_controller.js
172
+ - app/javascript/lostcoords/ui/controllers/lc_hovercard_controller.js
167
173
  - app/javascript/lostcoords/ui/controllers/lc_kbd_controller.js
168
174
  - app/javascript/lostcoords/ui/controllers/lc_menu_controller.js
169
175
  - app/javascript/lostcoords/ui/controllers/lc_nav_controller.js