phlex-cmdk 0.1.0 → 0.2.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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +59 -0
- data/README.md +166 -10
- data/assets/css/cmdk_base.css +47 -0
- data/assets/css/cmdk_themes.css +13 -0
- data/assets/js/cmdk.js +356 -54
- data/assets/js/cmdk_controller.js +6 -1
- data/lib/cmdk/base.rb +9 -1
- data/lib/cmdk/empty.rb +8 -4
- data/lib/cmdk/footer.rb +1 -5
- data/lib/cmdk/group.rb +23 -8
- data/lib/cmdk/item.rb +1 -1
- data/lib/cmdk/item_label.rb +25 -0
- data/lib/cmdk/list.rb +1 -1
- data/lib/cmdk/loading.rb +1 -1
- data/lib/cmdk/root.rb +15 -7
- data/lib/cmdk/version.rb +1 -1
- data/lib/cmdk.rb +9 -0
- metadata +3 -1
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
*
|
|
9
9
|
* Extend it and override the hooks you care about:
|
|
10
10
|
*
|
|
11
|
-
* import CmdkController from '
|
|
11
|
+
* import CmdkController from 'cmdk_controller' // pin to Cmdk.stimulus_controller_path
|
|
12
12
|
*
|
|
13
13
|
* export default class extends CmdkController {
|
|
14
14
|
* itemSelected({ detail: { value } }) { this.runCommand(value) }
|
|
@@ -41,6 +41,11 @@ export default class CmdkController extends Controller {
|
|
|
41
41
|
disconnect() {
|
|
42
42
|
for (const [type, listener] of this.#listeners) this.element.removeEventListener(type, listener)
|
|
43
43
|
this.#listeners = []
|
|
44
|
+
// Free the runtime's observers once the menu has left the document (a
|
|
45
|
+
// Turbo visit, frame reload or morph). A menu that is only moved — e.g.
|
|
46
|
+
// `data-turbo-permanent` — is still connected and keeps its state.
|
|
47
|
+
const root = this.root
|
|
48
|
+
if (root && !root.isConnected) Cmdk.destroy(root)
|
|
44
49
|
}
|
|
45
50
|
|
|
46
51
|
/** The [cmdk-root] this controller manages. */
|
data/lib/cmdk/base.rb
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
module Cmdk
|
|
2
2
|
# Shared base for all cmdk components.
|
|
3
3
|
class Base < ::Phlex::HTML
|
|
4
|
-
# Inline equivalent of cmdk's srOnlyStyles
|
|
4
|
+
# Inline equivalent of cmdk's srOnlyStyles. No longer rendered (since
|
|
5
|
+
# 0.2.0 the accessible label uses the `hidden` attribute, so the markup
|
|
6
|
+
# needs no inline style under a strict CSP); kept for code referencing it.
|
|
5
7
|
SR_ONLY_STYLE = 'position:absolute;width:1px;height:1px;padding:0;margin:-1px;' \
|
|
6
8
|
'overflow:hidden;clip:rect(0, 0, 0, 0);white-space:nowrap;border-width:0'.freeze
|
|
7
9
|
|
|
@@ -10,6 +12,12 @@ module Cmdk
|
|
|
10
12
|
# Merge user-supplied attributes into the component's defaults.
|
|
11
13
|
# User values win, except `class` and `style` which are concatenated
|
|
12
14
|
# and `data` hashes which are merged.
|
|
15
|
+
#
|
|
16
|
+
# Deliberately not Phlex's `mix`: `mix` treats *every* attribute as a
|
|
17
|
+
# token list (e.g. `mix({role: 'option'}, {role: 'button'})` yields
|
|
18
|
+
# `role: 'option button'`), which is wrong for scalar attributes like
|
|
19
|
+
# `role`/`type`/`tabindex`/`aria-selected` that should be replaced, not
|
|
20
|
+
# appended. Here only `class`/`style`/`data` combine; other scalars override.
|
|
13
21
|
def merged(defaults, overrides)
|
|
14
22
|
defaults.merge(overrides) do |key, default, override|
|
|
15
23
|
case key
|
data/lib/cmdk/empty.rb
CHANGED
|
@@ -1,15 +1,19 @@
|
|
|
1
1
|
module Cmdk
|
|
2
2
|
# Shown automatically when there are no results. Port of `<Command.Empty>`.
|
|
3
|
-
# Rendered hidden (inline
|
|
4
|
-
# the runtime toggles it based on the filtered result count
|
|
3
|
+
# Rendered with the `hidden` attribute (no inline style, so it works under a
|
|
4
|
+
# strict CSP); the runtime toggles it based on the filtered result count,
|
|
5
|
+
# with an inline display so theme CSS cannot override it. If your own CSS
|
|
6
|
+
# sets `display` on `[cmdk-empty]`, add `[cmdk-empty][hidden] { display:
|
|
7
|
+
# none }` so it stays hidden before the runtime starts (the shipped themes
|
|
8
|
+
# and base styles do this).
|
|
5
9
|
class Empty < Base
|
|
6
10
|
def initialize(**attributes)
|
|
7
11
|
@attributes = attributes
|
|
8
12
|
end
|
|
9
13
|
|
|
10
14
|
def view_template(&block)
|
|
11
|
-
defaults = { 'cmdk-empty' => '', role: 'presentation',
|
|
12
|
-
div(**merged(defaults, @attributes)
|
|
15
|
+
defaults = { 'cmdk-empty' => '', role: 'presentation', hidden: true }
|
|
16
|
+
div(**merged(defaults, @attributes), &block)
|
|
13
17
|
end
|
|
14
18
|
end
|
|
15
19
|
end
|
data/lib/cmdk/footer.rb
CHANGED
data/lib/cmdk/group.rb
CHANGED
|
@@ -1,31 +1,46 @@
|
|
|
1
|
-
require 'securerandom'
|
|
2
|
-
|
|
3
1
|
module Cmdk
|
|
4
2
|
# Groups items together with an optional heading. Port of `<Command.Group>`.
|
|
5
3
|
# Provide `value:` when there is no heading (it is used for sorting groups);
|
|
6
4
|
# with a string heading the value is inferred from it, matching React cmdk.
|
|
5
|
+
#
|
|
6
|
+
# `heading_attributes:` and `items_attributes:` add attributes (class,
|
|
7
|
+
# data-*, …) to the heading and to the `[cmdk-group-items]` wrapper; extra
|
|
8
|
+
# keyword attributes go to the group element itself.
|
|
7
9
|
class Group < Base
|
|
10
|
+
# Monotonic id source for `aria-labelledby` (the analog of React's useId,
|
|
11
|
+
# which this port mirrors). Deterministic and collision-free, unlike a
|
|
12
|
+
# random suffix; the mutex keeps it correct under threaded servers.
|
|
13
|
+
@heading_seq = 0
|
|
14
|
+
@heading_mutex = Mutex.new
|
|
15
|
+
|
|
16
|
+
def self.next_heading_id
|
|
17
|
+
@heading_mutex.synchronize { "cmdk-heading-#{@heading_seq += 1}" }
|
|
18
|
+
end
|
|
19
|
+
|
|
8
20
|
def initialize(heading: nil, value: nil, force_mount: false, scope: nil, scope_only: false,
|
|
9
|
-
server_filtered: false, **attributes)
|
|
21
|
+
server_filtered: false, heading_attributes: {}, items_attributes: {}, **attributes)
|
|
10
22
|
@heading = heading
|
|
11
23
|
@value = value
|
|
12
24
|
@force_mount = force_mount
|
|
13
25
|
@scope = scope
|
|
14
26
|
@scope_only = scope_only
|
|
15
27
|
@server_filtered = server_filtered
|
|
28
|
+
@heading_attributes = heading_attributes
|
|
29
|
+
@items_attributes = items_attributes
|
|
16
30
|
@attributes = attributes
|
|
17
31
|
end
|
|
18
32
|
|
|
19
33
|
def view_template(&block)
|
|
20
|
-
heading_id = @heading ?
|
|
34
|
+
heading_id = @heading ? (@heading_attributes[:id] || Group.next_heading_id) : nil
|
|
21
35
|
|
|
22
36
|
div(**merged(group_attributes, @attributes)) do
|
|
23
37
|
if @heading
|
|
24
|
-
div('cmdk-group-heading' => '', aria_hidden: 'true', id: heading_id
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
block ? block.call : nil
|
|
38
|
+
div(**merged({ 'cmdk-group-heading' => '', aria_hidden: 'true', id: heading_id }, @heading_attributes)) do
|
|
39
|
+
@heading
|
|
40
|
+
end
|
|
28
41
|
end
|
|
42
|
+
items = { 'cmdk-group-items' => '', role: 'group', aria_labelledby: heading_id }
|
|
43
|
+
div(**merged(items, @items_attributes), &block)
|
|
29
44
|
end
|
|
30
45
|
end
|
|
31
46
|
|
data/lib/cmdk/item.rb
CHANGED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
module Cmdk
|
|
2
|
+
# The part of an item whose matched characters the runtime highlights while
|
|
3
|
+
# searching (opt-in; an extension over the React API). Put the item's text
|
|
4
|
+
# in it; the runtime wraps each matched run in `<mark cmdk-match>` and
|
|
5
|
+
# restores the plain text when the search clears:
|
|
6
|
+
#
|
|
7
|
+
# Cmdk::Item(value: 'posts', href: '/posts') do
|
|
8
|
+
# icon
|
|
9
|
+
# Cmdk::ItemLabel() { 'Posts' }
|
|
10
|
+
# end
|
|
11
|
+
#
|
|
12
|
+
# Matches are computed against the label's own text with the default
|
|
13
|
+
# command-score algorithm. Keep the label to plain text: its children are
|
|
14
|
+
# replaced by text nodes and marks while highlighted. Style matches with
|
|
15
|
+
# `[cmdk-item-label] mark[cmdk-match]`.
|
|
16
|
+
class ItemLabel < Base
|
|
17
|
+
def initialize(**attributes)
|
|
18
|
+
@attributes = attributes
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def view_template(&block)
|
|
22
|
+
span(**merged({ 'cmdk-item-label' => '' }, @attributes), &block)
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
data/lib/cmdk/list.rb
CHANGED
|
@@ -10,7 +10,7 @@ module Cmdk
|
|
|
10
10
|
|
|
11
11
|
def view_template(&block)
|
|
12
12
|
div(**merged({ 'cmdk-list' => '', role: 'listbox', tabindex: '-1', aria_label: @label }, @attributes)) do
|
|
13
|
-
div('cmdk-list-sizer' => ''
|
|
13
|
+
div('cmdk-list-sizer' => '', &block)
|
|
14
14
|
end
|
|
15
15
|
end
|
|
16
16
|
end
|
data/lib/cmdk/loading.rb
CHANGED
data/lib/cmdk/root.rb
CHANGED
|
@@ -14,10 +14,19 @@ module Cmdk
|
|
|
14
14
|
# pill before the input. The rest of the input is then matched only against
|
|
15
15
|
# items/groups tagged with the same `scope:`. Pass `active_scope:` to
|
|
16
16
|
# server-render an already-pinned scope.
|
|
17
|
+
#
|
|
18
|
+
# The accessible `label:` renders as a `hidden` <label cmdk-label> that the
|
|
19
|
+
# runtime wires to the input via `aria-labelledby` (no inline style, so it
|
|
20
|
+
# works under a strict CSP). `label_attributes:` adds attributes to it
|
|
21
|
+
# (`label_attributes: { hidden: false, class: 'my-label' }` shows it).
|
|
22
|
+
#
|
|
23
|
+
# `native_home_end: true` lets Home/End move the caret inside the input
|
|
24
|
+
# instead of jumping to the first/last item (⌘↑/⌘↓ still do that).
|
|
17
25
|
class Root < Base
|
|
18
26
|
def initialize(label: nil, default_value: nil, should_filter: true, loop: false,
|
|
19
27
|
vim_bindings: true, disable_pointer_selection: false, scopes: nil,
|
|
20
|
-
scope_picker: nil, active_scope: nil,
|
|
28
|
+
scope_picker: nil, active_scope: nil, label_attributes: {},
|
|
29
|
+
native_home_end: false, **attributes)
|
|
21
30
|
@label = label
|
|
22
31
|
@default_value = default_value
|
|
23
32
|
@should_filter = should_filter
|
|
@@ -27,22 +36,20 @@ module Cmdk
|
|
|
27
36
|
@scopes = scopes
|
|
28
37
|
@scope_picker = scope_picker
|
|
29
38
|
@active_scope = active_scope
|
|
39
|
+
@label_attributes = label_attributes
|
|
40
|
+
@native_home_end = native_home_end
|
|
30
41
|
@attributes = attributes
|
|
31
42
|
end
|
|
32
43
|
|
|
33
44
|
def view_template(&block)
|
|
34
45
|
div(**merged(root_attributes, @attributes)) do
|
|
35
|
-
label('cmdk-label' => '',
|
|
36
|
-
|
|
46
|
+
label(**merged({ 'cmdk-label' => '', hidden: true }, @label_attributes)) { @label }
|
|
47
|
+
block&.call
|
|
37
48
|
end
|
|
38
49
|
end
|
|
39
50
|
|
|
40
51
|
private
|
|
41
52
|
|
|
42
|
-
def yield_content(&block)
|
|
43
|
-
block ? block.call : nil
|
|
44
|
-
end
|
|
45
|
-
|
|
46
53
|
def root_attributes
|
|
47
54
|
data = {}
|
|
48
55
|
data[:cmdk_should_filter] = 'false' unless @should_filter
|
|
@@ -52,6 +59,7 @@ module Cmdk
|
|
|
52
59
|
data[:cmdk_default_value] = @default_value if @default_value
|
|
53
60
|
data[:cmdk_scopes] = Array(@scopes).join(' ') if @scopes
|
|
54
61
|
data[:cmdk_active_scope] = @active_scope if @active_scope
|
|
62
|
+
data[:cmdk_native_home_end] = '' if @native_home_end
|
|
55
63
|
unless @scope_picker.nil?
|
|
56
64
|
data[:cmdk_scope_picker] = @scope_picker == false ? 'false' : @scope_picker
|
|
57
65
|
end
|
data/lib/cmdk/version.rb
CHANGED
data/lib/cmdk.rb
CHANGED
|
@@ -19,6 +19,14 @@ module Cmdk
|
|
|
19
19
|
File.expand_path('../assets/css/cmdk_themes.css', __dir__)
|
|
20
20
|
end
|
|
21
21
|
|
|
22
|
+
# Absolute path to the runtime's base styles as a static stylesheet (the
|
|
23
|
+
# same rules the runtime injects at start, all zero-specificity `:where()`).
|
|
24
|
+
# For CSP-strict or cascade-layered hosts that turn the injection off with
|
|
25
|
+
# `<meta name="cmdk-base-styles" content="off">` and include it themselves.
|
|
26
|
+
def self.base_stylesheet_path
|
|
27
|
+
File.expand_path('../assets/css/cmdk_base.css', __dir__)
|
|
28
|
+
end
|
|
29
|
+
|
|
22
30
|
# Absolute path to the optional Stimulus base controller (requires
|
|
23
31
|
# @hotwired/stimulus and the runtime pinned/aliased as `cmdk`).
|
|
24
32
|
def self.stimulus_controller_path
|
|
@@ -32,6 +40,7 @@ require_relative 'cmdk/root'
|
|
|
32
40
|
require_relative 'cmdk/input'
|
|
33
41
|
require_relative 'cmdk/list'
|
|
34
42
|
require_relative 'cmdk/item'
|
|
43
|
+
require_relative 'cmdk/item_label'
|
|
35
44
|
require_relative 'cmdk/group'
|
|
36
45
|
require_relative 'cmdk/separator'
|
|
37
46
|
require_relative 'cmdk/empty'
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: phlex-cmdk
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.2.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Leon Gieser
|
|
@@ -42,6 +42,7 @@ files:
|
|
|
42
42
|
- CHANGELOG.md
|
|
43
43
|
- LICENSE.md
|
|
44
44
|
- README.md
|
|
45
|
+
- assets/css/cmdk_base.css
|
|
45
46
|
- assets/css/cmdk_themes.css
|
|
46
47
|
- assets/js/cmdk.js
|
|
47
48
|
- assets/js/cmdk_controller.js
|
|
@@ -53,6 +54,7 @@ files:
|
|
|
53
54
|
- lib/cmdk/group.rb
|
|
54
55
|
- lib/cmdk/input.rb
|
|
55
56
|
- lib/cmdk/item.rb
|
|
57
|
+
- lib/cmdk/item_label.rb
|
|
56
58
|
- lib/cmdk/list.rb
|
|
57
59
|
- lib/cmdk/loading.rb
|
|
58
60
|
- lib/cmdk/root.rb
|