nitro_kit 0.9.0 → 2.0.0.alpha.2
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/.agents/plugins/marketplace.json +20 -0
- data/CHANGELOG.md +172 -0
- data/LICENSE +28 -0
- data/README.md +22 -15
- data/STYLE_GUIDE.md +425 -0
- data/app/assets/stylesheets/nitro_kit-tailwind-v4.css +38 -0
- data/app/assets/stylesheets/nitro_kit.css +7239 -0
- data/app/components/nitro_kit/accordion.rb +125 -65
- data/app/components/nitro_kit/alert.rb +80 -43
- data/app/components/nitro_kit/app_navigation.rb +307 -0
- data/app/components/nitro_kit/app_shell.rb +227 -0
- data/app/components/nitro_kit/appearance_bootstrap.rb +146 -0
- data/app/components/nitro_kit/appearance_picker.rb +156 -0
- data/app/components/nitro_kit/auth_shell.rb +34 -0
- data/app/components/nitro_kit/avatar.rb +85 -29
- data/app/components/nitro_kit/avatar_stack.rb +155 -9
- data/app/components/nitro_kit/badge.rb +43 -81
- data/app/components/nitro_kit/button.rb +180 -103
- data/app/components/nitro_kit/button_group.rb +63 -11
- data/app/components/nitro_kit/button_to.rb +69 -0
- data/app/components/nitro_kit/card.rb +42 -45
- data/app/components/nitro_kit/checkbox.rb +115 -62
- data/app/components/nitro_kit/checkbox_group.rb +131 -22
- data/app/components/nitro_kit/choice.rb +55 -0
- data/app/components/nitro_kit/combobox.rb +292 -105
- data/app/components/nitro_kit/command_palette.rb +412 -0
- data/app/components/nitro_kit/component.rb +322 -46
- data/app/components/nitro_kit/container.rb +32 -0
- data/app/components/nitro_kit/control_group.rb +45 -0
- data/app/components/nitro_kit/danger_zone.rb +78 -0
- data/app/components/nitro_kit/data_section.rb +106 -0
- data/app/components/nitro_kit/details_table.rb +229 -0
- data/app/components/nitro_kit/dialog.rb +257 -83
- data/app/components/nitro_kit/dropdown.rb +335 -122
- data/app/components/nitro_kit/dropzone.rb +326 -0
- data/app/components/nitro_kit/empty_state.rb +99 -0
- data/app/components/nitro_kit/field.rb +461 -236
- data/app/components/nitro_kit/field_group.rb +15 -8
- data/app/components/nitro_kit/fieldset.rb +37 -39
- data/app/components/nitro_kit/flex.rb +49 -0
- data/app/components/nitro_kit/form_builder.rb +313 -72
- data/app/components/nitro_kit/form_section.rb +79 -0
- data/app/components/nitro_kit/grid.rb +34 -0
- data/app/components/nitro_kit/icon.rb +54 -25
- data/app/components/nitro_kit/input.rb +97 -10
- data/app/components/nitro_kit/label.rb +22 -6
- data/app/components/nitro_kit/layout_options.rb +7 -0
- data/app/components/nitro_kit/page_header.rb +76 -0
- data/app/components/nitro_kit/pagination.rb +355 -69
- data/app/components/nitro_kit/pagination_bar.rb +90 -0
- data/app/components/nitro_kit/progressive_image.rb +181 -0
- data/app/components/nitro_kit/radio_button.rb +77 -55
- data/app/components/nitro_kit/radio_button_group.rb +107 -36
- data/app/components/nitro_kit/responsive_value.rb +94 -0
- data/app/components/nitro_kit/rich_text_area.rb +31 -0
- data/app/components/nitro_kit/select.rb +129 -64
- data/app/components/nitro_kit/settings_layout.rb +152 -0
- data/app/components/nitro_kit/sheet.rb +202 -0
- data/app/components/nitro_kit/stat_grid.rb +75 -0
- data/app/components/nitro_kit/switch.rb +108 -51
- data/app/components/nitro_kit/table.rb +237 -45
- data/app/components/nitro_kit/tabs.rb +168 -63
- data/app/components/nitro_kit/textarea.rb +81 -10
- data/app/components/nitro_kit/toast.rb +211 -80
- data/app/components/nitro_kit/toolbar.rb +75 -0
- data/app/components/nitro_kit/tooltip.rb +235 -34
- data/app/components/nitro_kit/typeset.rb +28 -0
- data/app/javascript/controllers/nk/app_shell_controller.js +224 -0
- data/app/javascript/controllers/nk/appearance_controller.js +73 -0
- data/app/javascript/controllers/nk/avatar_controller.js +15 -0
- data/app/javascript/controllers/nk/button_controller.js +56 -0
- data/app/javascript/controllers/nk/checkable_controller.js +41 -0
- data/app/javascript/controllers/nk/combobox_controller.js +314 -82
- data/app/javascript/controllers/nk/command_palette_controller.js +249 -0
- data/app/javascript/controllers/nk/dialog_controller.js +14 -10
- data/app/javascript/controllers/nk/dropdown_controller.js +106 -58
- data/app/javascript/controllers/nk/dropzone/direct_upload.js +61 -0
- data/app/javascript/controllers/nk/dropzone/file_rules.js +77 -0
- data/app/javascript/controllers/nk/dropzone/form_submit_lock.js +30 -0
- data/app/javascript/controllers/nk/dropzone_controller.js +482 -0
- data/app/javascript/controllers/nk/overlay_position.js +37 -0
- data/app/javascript/controllers/nk/progressive_image_controller.js +116 -0
- data/app/javascript/controllers/nk/tabs_controller.js +135 -23
- data/app/javascript/controllers/nk/toast_controller.js +112 -45
- data/app/javascript/controllers/nk/tooltip_controller.js +12 -47
- data/config/importmap.rb +2 -0
- data/config/locales/en.yml +98 -0
- data/docs/agent_guide.md +139 -0
- data/docs/agent_native_spec.md +422 -0
- data/docs/component_contracts.md +263 -0
- data/docs/customization.md +595 -0
- data/docs/hotwire.md +94 -0
- data/docs/initialization_prompt.md +36 -0
- data/docs/migration_1_to_2.md +318 -0
- data/docs/new_app_strategy.md +22 -0
- data/docs/patterns/application_foundation.md +136 -0
- data/docs/patterns/crud_resource.md +144 -0
- data/docs/patterns/destructive_action.md +105 -0
- data/docs/patterns/flash_and_toast.md +57 -0
- data/docs/patterns/inline_edit.md +59 -0
- data/docs/patterns/queryable_collection.md +187 -0
- data/docs/patterns/resource_form.md +126 -0
- data/docs/rails_conventions.md +95 -0
- data/docs/rails_integration.md +514 -0
- data/lib/generators/nitro_kit/install_generator.rb +48 -0
- data/lib/generators/nitro_kit/upgrade_smoke_tests_generator.rb +67 -0
- data/lib/nitro_kit/engine.rb +20 -0
- data/lib/nitro_kit/installation.rb +671 -0
- data/lib/nitro_kit/migration_inventory.rb +345 -0
- data/lib/nitro_kit/upgrade_smoke_test.rb +403 -0
- data/lib/nitro_kit/version.rb +1 -1
- data/lib/nitro_kit.rb +5 -42
- data/lib/rails/commands/nitro_kit/nitro_kit_command.rb +42 -0
- data/lib/tasks/nitro_kit_tasks.rake +79 -4
- data/plugins/nitro-kit/.codex-plugin/plugin.json +28 -0
- data/plugins/nitro-kit/skills/nitro-kit-hotwire/SKILL.md +67 -0
- data/plugins/nitro-kit/skills/nitro-kit-hotwire/agents/openai.yaml +4 -0
- data/plugins/nitro-kit/skills/nitro-kit-rails/SKILL.md +60 -0
- data/plugins/nitro-kit/skills/nitro-kit-rails/agents/openai.yaml +4 -0
- data/plugins/nitro-kit/skills/nitro-kit-ui/SKILL.md +62 -0
- data/plugins/nitro-kit/skills/nitro-kit-ui/agents/openai.yaml +4 -0
- data/src/stylesheets/nitro_kit/components/accordion.css +119 -0
- data/src/stylesheets/nitro_kit/components/alert.css +82 -0
- data/src/stylesheets/nitro_kit/components/app_navigation.css +156 -0
- data/src/stylesheets/nitro_kit/components/app_shell.css +605 -0
- data/src/stylesheets/nitro_kit/components/appearance_picker.css +117 -0
- data/src/stylesheets/nitro_kit/components/auth_shell.css +8 -0
- data/src/stylesheets/nitro_kit/components/avatar.css +81 -0
- data/src/stylesheets/nitro_kit/components/avatar_stack.css +80 -0
- data/src/stylesheets/nitro_kit/components/badge.css +76 -0
- data/src/stylesheets/nitro_kit/components/button.css +283 -0
- data/src/stylesheets/nitro_kit/components/button_group.css +53 -0
- data/src/stylesheets/nitro_kit/components/button_to.css +6 -0
- data/src/stylesheets/nitro_kit/components/card.css +70 -0
- data/src/stylesheets/nitro_kit/components/checkbox.css +160 -0
- data/src/stylesheets/nitro_kit/components/checkbox_group.css +61 -0
- data/src/stylesheets/nitro_kit/components/combobox.css +148 -0
- data/src/stylesheets/nitro_kit/components/command_palette.css +255 -0
- data/src/stylesheets/nitro_kit/components/container.css +25 -0
- data/src/stylesheets/nitro_kit/components/control_group.css +149 -0
- data/src/stylesheets/nitro_kit/components/danger_zone.css +54 -0
- data/src/stylesheets/nitro_kit/components/data_section.css +40 -0
- data/src/stylesheets/nitro_kit/components/details_table.css +53 -0
- data/src/stylesheets/nitro_kit/components/dialog.css +80 -0
- data/src/stylesheets/nitro_kit/components/dropdown.css +152 -0
- data/src/stylesheets/nitro_kit/components/dropzone.css +198 -0
- data/src/stylesheets/nitro_kit/components/empty_state.css +53 -0
- data/src/stylesheets/nitro_kit/components/field.css +79 -0
- data/src/stylesheets/nitro_kit/components/field_group.css +6 -0
- data/src/stylesheets/nitro_kit/components/fieldset.css +28 -0
- data/src/stylesheets/nitro_kit/components/flex.css +455 -0
- data/src/stylesheets/nitro_kit/components/form_section.css +71 -0
- data/src/stylesheets/nitro_kit/components/grid.css +308 -0
- data/src/stylesheets/nitro_kit/components/icon.css +26 -0
- data/src/stylesheets/nitro_kit/components/input.css +80 -0
- data/src/stylesheets/nitro_kit/components/label.css +9 -0
- data/src/stylesheets/nitro_kit/components/layout.css +440 -0
- data/src/stylesheets/nitro_kit/components/page_header.css +61 -0
- data/src/stylesheets/nitro_kit/components/pagination.css +119 -0
- data/src/stylesheets/nitro_kit/components/pagination_bar.css +41 -0
- data/src/stylesheets/nitro_kit/components/palette.css +113 -0
- data/src/stylesheets/nitro_kit/components/progressive_image.css +139 -0
- data/src/stylesheets/nitro_kit/components/radio_button.css +127 -0
- data/src/stylesheets/nitro_kit/components/radio_button_group.css +143 -0
- data/src/stylesheets/nitro_kit/components/rich_text_area.css +21 -0
- data/src/stylesheets/nitro_kit/components/select.css +70 -0
- data/src/stylesheets/nitro_kit/components/settings_layout.css +100 -0
- data/src/stylesheets/nitro_kit/components/sheet.css +100 -0
- data/src/stylesheets/nitro_kit/components/stat_grid.css +51 -0
- data/src/stylesheets/nitro_kit/components/switch.css +152 -0
- data/src/stylesheets/nitro_kit/components/table.css +116 -0
- data/src/stylesheets/nitro_kit/components/tabs.css +138 -0
- data/src/stylesheets/nitro_kit/components/textarea.css +39 -0
- data/src/stylesheets/nitro_kit/components/toast.css +131 -0
- data/src/stylesheets/nitro_kit/components/toolbar.css +78 -0
- data/src/stylesheets/nitro_kit/components/tooltip.css +124 -0
- data/src/stylesheets/nitro_kit/components/typeset.css +191 -0
- data/src/stylesheets/nitro_kit/layers.css +7 -0
- data/src/stylesheets/nitro_kit/reset.css +192 -0
- data/src/stylesheets/nitro_kit/tokens.css +198 -0
- metadata +182 -51
- data/Rakefile +0 -8
- data/app/assets/tailwind/application.css +0 -49
- data/app/components/nitro_kit/datepicker.rb +0 -9
- data/app/helpers/nitro_kit/accordion_helper.rb +0 -9
- data/app/helpers/nitro_kit/alert_helper.rb +0 -11
- data/app/helpers/nitro_kit/avatar_helper.rb +0 -13
- data/app/helpers/nitro_kit/badge_helper.rb +0 -13
- data/app/helpers/nitro_kit/button_group_helper.rb +0 -9
- data/app/helpers/nitro_kit/button_helper.rb +0 -49
- data/app/helpers/nitro_kit/card_helper.rb +0 -9
- data/app/helpers/nitro_kit/checkbox_helper.rb +0 -50
- data/app/helpers/nitro_kit/combobox_helper.rb +0 -9
- data/app/helpers/nitro_kit/datepicker_helper.rb +0 -9
- data/app/helpers/nitro_kit/dialog_helper.rb +0 -9
- data/app/helpers/nitro_kit/dropdown_helper.rb +0 -9
- data/app/helpers/nitro_kit/field_group_helper.rb +0 -9
- data/app/helpers/nitro_kit/field_helper.rb +0 -9
- data/app/helpers/nitro_kit/fieldset_helper.rb +0 -9
- data/app/helpers/nitro_kit/form_helper.rb +0 -13
- data/app/helpers/nitro_kit/icon_helper.rb +0 -9
- data/app/helpers/nitro_kit/input_helper.rb +0 -37
- data/app/helpers/nitro_kit/label_helper.rb +0 -19
- data/app/helpers/nitro_kit/pagination_helper.rb +0 -46
- data/app/helpers/nitro_kit/radio_button_helper.rb +0 -23
- data/app/helpers/nitro_kit/select_helper.rb +0 -24
- data/app/helpers/nitro_kit/switch_helper.rb +0 -9
- data/app/helpers/nitro_kit/table_helper.rb +0 -9
- data/app/helpers/nitro_kit/tabs_helper.rb +0 -9
- data/app/helpers/nitro_kit/textarea_helper.rb +0 -9
- data/app/helpers/nitro_kit/toast_helper.rb +0 -25
- data/app/helpers/nitro_kit/tooltip_helper.rb +0 -9
- data/app/javascript/controllers/nk/accordion_controller.js +0 -18
- data/app/javascript/controllers/nk/datepicker_controller.js +0 -5
- data/app/javascript/controllers/nk/switch_controller.js +0 -32
- data/lib/generators/nitro_kit/component_generator.rb +0 -84
- data/lib/nitro_kit/schema_builder.rb +0 -129
- data/lib/nitro_kit/variants.rb +0 -21
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module NitroKit
|
|
4
|
+
class ProgressiveImage < Component
|
|
5
|
+
include Phlex::Rails::Helpers::Routes
|
|
6
|
+
|
|
7
|
+
SIZES = %i[sm md lg].freeze
|
|
8
|
+
PIXELS = { sm: 320, md: 720, lg: 1_440 }.freeze
|
|
9
|
+
PLACEHOLDER_PIXELS = 48
|
|
10
|
+
|
|
11
|
+
def initialize(
|
|
12
|
+
attachment:,
|
|
13
|
+
alt: nil,
|
|
14
|
+
size: :md,
|
|
15
|
+
decorative: false,
|
|
16
|
+
id: nil,
|
|
17
|
+
html: {},
|
|
18
|
+
aria: {},
|
|
19
|
+
data: {},
|
|
20
|
+
desperately_need_a_class: nil
|
|
21
|
+
)
|
|
22
|
+
@attachment = validate_attachment!(attachment)
|
|
23
|
+
@size = validate_choice!(:size, size, SIZES)
|
|
24
|
+
@decorative = validate_boolean!(:decorative, decorative)
|
|
25
|
+
@attached = attachment&.attached? || false
|
|
26
|
+
@alt = validate_alt!(alt)
|
|
27
|
+
|
|
28
|
+
validate_attached_contract! if @attached
|
|
29
|
+
|
|
30
|
+
super(
|
|
31
|
+
component: :progressive_image,
|
|
32
|
+
attributes: {
|
|
33
|
+
id:,
|
|
34
|
+
aria: { busy: @attached ? true : nil },
|
|
35
|
+
data: {
|
|
36
|
+
state: @attached ? "loading" : "empty",
|
|
37
|
+
controller: @attached ? "nk--progressive-image" : nil
|
|
38
|
+
}.compact
|
|
39
|
+
},
|
|
40
|
+
html:,
|
|
41
|
+
aria:,
|
|
42
|
+
data:,
|
|
43
|
+
size:,
|
|
44
|
+
desperately_need_a_class:
|
|
45
|
+
)
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
attr_reader :attachment, :alt, :size, :decorative
|
|
49
|
+
|
|
50
|
+
def view_template
|
|
51
|
+
div(**root_attributes) do
|
|
52
|
+
if @attached
|
|
53
|
+
render_placeholder
|
|
54
|
+
render_image
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
render_fallback
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
private
|
|
62
|
+
|
|
63
|
+
def render_placeholder
|
|
64
|
+
img(
|
|
65
|
+
**slot_attributes(
|
|
66
|
+
:placeholder,
|
|
67
|
+
attributes: image_dimensions.merge(
|
|
68
|
+
src: variant_url(PLACEHOLDER_PIXELS),
|
|
69
|
+
alt: "",
|
|
70
|
+
loading: "eager",
|
|
71
|
+
decoding: "async"
|
|
72
|
+
),
|
|
73
|
+
aria: { hidden: true }
|
|
74
|
+
)
|
|
75
|
+
)
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def render_image
|
|
79
|
+
pixels = PIXELS.fetch(size)
|
|
80
|
+
image_url = variant_url(pixels)
|
|
81
|
+
|
|
82
|
+
img(
|
|
83
|
+
**slot_attributes(
|
|
84
|
+
:image,
|
|
85
|
+
attributes: image_dimensions.merge(
|
|
86
|
+
src: image_url,
|
|
87
|
+
srcset: "#{image_url} 1x, #{variant_url(pixels * 2)} 2x",
|
|
88
|
+
alt:,
|
|
89
|
+
loading: "lazy",
|
|
90
|
+
decoding: "async",
|
|
91
|
+
data: { nk__progressive_image_target: "image" }
|
|
92
|
+
)
|
|
93
|
+
)
|
|
94
|
+
)
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
def render_fallback
|
|
98
|
+
visible = !@attached
|
|
99
|
+
accessible = !decorative
|
|
100
|
+
|
|
101
|
+
span(
|
|
102
|
+
**slot_attributes(
|
|
103
|
+
:fallback,
|
|
104
|
+
attributes: {
|
|
105
|
+
hidden: visible ? nil : true,
|
|
106
|
+
# Only an attached image can change state after render, so only
|
|
107
|
+
# that fallback is a live region.
|
|
108
|
+
role: (accessible && @attached) ? "status" : nil
|
|
109
|
+
},
|
|
110
|
+
aria: {
|
|
111
|
+
hidden: accessible ? nil : true
|
|
112
|
+
},
|
|
113
|
+
data: @attached ? { nk__progressive_image_target: "fallback" } : {}
|
|
114
|
+
)
|
|
115
|
+
) { fallback_text }
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
def fallback_text
|
|
119
|
+
alt.strip.empty? ? I18n.t("nitro_kit.progressive_image.unavailable") : alt
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
def variant_url(pixels)
|
|
123
|
+
representation = attachment.variant(resize_to_limit: [ pixels, pixels ])
|
|
124
|
+
return representation if representation.is_a?(String)
|
|
125
|
+
|
|
126
|
+
url_for(representation)
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
def image_dimensions
|
|
130
|
+
blob = attachment.blob
|
|
131
|
+
return {} if blob.respond_to?(:analyzed?) && !blob.analyzed?
|
|
132
|
+
|
|
133
|
+
metadata = blob.metadata
|
|
134
|
+
return {} unless metadata.is_a?(Hash)
|
|
135
|
+
|
|
136
|
+
width = metadata["width"] || metadata[:width]
|
|
137
|
+
height = metadata["height"] || metadata[:height]
|
|
138
|
+
return {} unless positive_dimension?(width) && positive_dimension?(height)
|
|
139
|
+
|
|
140
|
+
{ width:, height: }
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
def positive_dimension?(value)
|
|
144
|
+
value.is_a?(Numeric) && value.positive?
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
def validate_attachment!(value)
|
|
148
|
+
return value if value.nil? || value.respond_to?(:attached?)
|
|
149
|
+
|
|
150
|
+
raise ArgumentError, "attachment must be nil or an Active Storage attachment"
|
|
151
|
+
end
|
|
152
|
+
|
|
153
|
+
def validate_attached_contract!
|
|
154
|
+
missing = %i[variant blob].reject { |method| attachment.respond_to?(method) }
|
|
155
|
+
unless missing.empty?
|
|
156
|
+
raise ArgumentError, "attached image must expose #{missing.join(' and ')}"
|
|
157
|
+
end
|
|
158
|
+
|
|
159
|
+
blob = attachment.blob
|
|
160
|
+
unless blob.respond_to?(:image?) && blob.image? && blob.respond_to?(:variable?) && blob.variable?
|
|
161
|
+
raise ArgumentError, "attached image must have an image blob that supports variants"
|
|
162
|
+
end
|
|
163
|
+
end
|
|
164
|
+
|
|
165
|
+
def validate_alt!(value)
|
|
166
|
+
unless value.nil? || value.is_a?(String)
|
|
167
|
+
raise ArgumentError, "alt must be a String or nil"
|
|
168
|
+
end
|
|
169
|
+
return "" if decorative
|
|
170
|
+
|
|
171
|
+
if value.nil?
|
|
172
|
+
raise ArgumentError, "alt: is required unless decorative: true"
|
|
173
|
+
end
|
|
174
|
+
if @attached && value.strip.empty?
|
|
175
|
+
raise ArgumentError, "attached non-decorative images require non-blank alt text"
|
|
176
|
+
end
|
|
177
|
+
|
|
178
|
+
value
|
|
179
|
+
end
|
|
180
|
+
end
|
|
181
|
+
end
|
|
@@ -2,34 +2,75 @@
|
|
|
2
2
|
|
|
3
3
|
module NitroKit
|
|
4
4
|
class RadioButton < Component
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
5
|
+
SIZES = %i[md lg].freeze
|
|
6
|
+
|
|
7
|
+
def initialize(
|
|
8
|
+
label: nil,
|
|
9
|
+
description: nil,
|
|
10
|
+
id: nil,
|
|
11
|
+
name: nil,
|
|
12
|
+
value: "1",
|
|
13
|
+
checked: false,
|
|
14
|
+
disabled: false,
|
|
15
|
+
required: false,
|
|
16
|
+
invalid: false,
|
|
17
|
+
size: :md,
|
|
18
|
+
html: {},
|
|
19
|
+
aria: {},
|
|
20
|
+
data: {},
|
|
21
|
+
control_html: {},
|
|
22
|
+
control_aria: {},
|
|
23
|
+
control_data: {},
|
|
24
|
+
desperately_need_a_class: nil
|
|
25
|
+
)
|
|
26
|
+
@label = validate_optional_text!(:label, label)
|
|
27
|
+
@description = validate_optional_text!(:description, description)
|
|
28
|
+
@id = id
|
|
29
|
+
@name = name
|
|
30
|
+
@value = value
|
|
31
|
+
@checked = validate_boolean!(:checked, checked)
|
|
32
|
+
@disabled = validate_boolean!(:disabled, disabled)
|
|
33
|
+
@required = validate_boolean!(:required, required)
|
|
34
|
+
@invalid = validate_boolean!(:invalid, invalid)
|
|
35
|
+
@size = validate_choice!(:size, size, SIZES)
|
|
36
|
+
@control_html = control_html
|
|
37
|
+
@control_aria = control_aria
|
|
38
|
+
@control_data = control_data
|
|
39
|
+
|
|
40
|
+
if description && (!id.is_a?(String) || id.strip.empty?)
|
|
41
|
+
raise ArgumentError, "radio button requires a non-blank String id when description is present"
|
|
42
|
+
end
|
|
10
43
|
|
|
11
44
|
super(
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
45
|
+
component: :radio_button,
|
|
46
|
+
size: @size,
|
|
47
|
+
attributes: {
|
|
48
|
+
data: { disabled: @disabled ? "true" : nil }.compact
|
|
49
|
+
},
|
|
50
|
+
html:,
|
|
51
|
+
aria:,
|
|
52
|
+
data:,
|
|
53
|
+
desperately_need_a_class:
|
|
16
54
|
)
|
|
17
55
|
end
|
|
18
56
|
|
|
19
|
-
|
|
57
|
+
attr_reader :label, :description, :id, :name, :value, :size
|
|
20
58
|
|
|
21
|
-
|
|
59
|
+
def view_template(&block)
|
|
60
|
+
require_accessible_name!(&block)
|
|
22
61
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
62
|
+
div(**root_attributes) do
|
|
63
|
+
if label.nil? && !block
|
|
64
|
+
render_control
|
|
65
|
+
span(**slot_attributes(:indicator), aria: { hidden: "true" })
|
|
66
|
+
else
|
|
67
|
+
render_in_slot(Label.new(for: id), :label) do
|
|
68
|
+
render_control
|
|
69
|
+
span(**slot_attributes(:indicator), aria: { hidden: "true" })
|
|
70
|
+
span(**slot_attributes(:content)) do
|
|
71
|
+
span(**slot_attributes(:label_text)) { text_or_block(label, &block) }
|
|
72
|
+
span(**slot_attributes(:description, attributes: { id: description_id })) { plain(description) } if description
|
|
73
|
+
end
|
|
33
74
|
end
|
|
34
75
|
end
|
|
35
76
|
end
|
|
@@ -37,41 +78,22 @@ module NitroKit
|
|
|
37
78
|
|
|
38
79
|
private
|
|
39
80
|
|
|
40
|
-
def
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
81
|
+
def render_control
|
|
82
|
+
render_in_slot(
|
|
83
|
+
Input.new(
|
|
84
|
+
type: :radio,
|
|
85
|
+
id:,
|
|
86
|
+
name:,
|
|
87
|
+
value:,
|
|
88
|
+
checked: @checked,
|
|
89
|
+
disabled: @disabled,
|
|
90
|
+
required: @required,
|
|
91
|
+
html: @control_html,
|
|
92
|
+
aria: control_aria,
|
|
93
|
+
data: @control_data
|
|
45
94
|
),
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
stroke: "none"
|
|
49
|
-
) do |svg|
|
|
50
|
-
svg.circle(cx: 10, cy: 10, r: 10)
|
|
51
|
-
end
|
|
52
|
-
end
|
|
53
|
-
|
|
54
|
-
def wrapper_class
|
|
55
|
-
"inline-flex items-center gap-2"
|
|
56
|
-
end
|
|
57
|
-
|
|
58
|
-
def input_class
|
|
59
|
-
[
|
|
60
|
-
"peer appearance-none shadow-sm rounded-full border text-foreground bg-background",
|
|
61
|
-
"[&[aria-checked='true']]:bg-primary",
|
|
62
|
-
"focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring"
|
|
63
|
-
]
|
|
64
|
-
end
|
|
65
|
-
|
|
66
|
-
def size_class
|
|
67
|
-
case size
|
|
68
|
-
when :md
|
|
69
|
-
"[&>input]:size-5 [&>svg]:size-2.5"
|
|
70
|
-
when :lg
|
|
71
|
-
"[&>input]:size-7 [&>svg]:size-3.5"
|
|
72
|
-
else
|
|
73
|
-
raise ArgumentError, "Unknown size `#{size}'"
|
|
74
|
-
end
|
|
95
|
+
:control
|
|
96
|
+
)
|
|
75
97
|
end
|
|
76
98
|
end
|
|
77
99
|
end
|
|
@@ -2,56 +2,127 @@
|
|
|
2
2
|
|
|
3
3
|
module NitroKit
|
|
4
4
|
class RadioButtonGroup < Component
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
ORIENTATIONS = %i[vertical horizontal].freeze
|
|
6
|
+
PRESENTATIONS = %i[list cards segmented].freeze
|
|
7
7
|
|
|
8
|
-
|
|
9
|
-
|
|
8
|
+
alias :html_legend :legend
|
|
9
|
+
|
|
10
|
+
def initialize(
|
|
11
|
+
legend:,
|
|
12
|
+
options:,
|
|
13
|
+
name:,
|
|
14
|
+
value: nil,
|
|
15
|
+
id: nil,
|
|
16
|
+
description: nil,
|
|
17
|
+
orientation: :vertical,
|
|
18
|
+
presentation: :list,
|
|
19
|
+
disabled: false,
|
|
20
|
+
required: false,
|
|
21
|
+
size: :md,
|
|
22
|
+
html: {},
|
|
23
|
+
aria: {},
|
|
24
|
+
data: {},
|
|
25
|
+
desperately_need_a_class: nil
|
|
26
|
+
)
|
|
27
|
+
@legend = validate_text!(:legend, legend)
|
|
28
|
+
@options = Array(options).map { |choice| Choice.coerce(choice) }.freeze
|
|
29
|
+
@name = validate_text!(:name, name)
|
|
30
|
+
@value = value
|
|
31
|
+
@id = id || deterministic_id(name)
|
|
32
|
+
@description = validate_optional_text!(:description, description)
|
|
33
|
+
@orientation = validate_choice!(:orientation, orientation, ORIENTATIONS)
|
|
34
|
+
@presentation = validate_choice!(:presentation, presentation, PRESENTATIONS)
|
|
35
|
+
@disabled = validate_boolean!(:disabled, disabled)
|
|
36
|
+
@required = validate_boolean!(:required, required)
|
|
37
|
+
@size = validate_choice!(:size, size, RadioButton::SIZES)
|
|
38
|
+
|
|
39
|
+
raise ArgumentError, "options cannot be empty" if @options.empty?
|
|
40
|
+
validate_unique_choices!
|
|
10
41
|
|
|
11
42
|
super(
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
43
|
+
component: :radio_button_group,
|
|
44
|
+
size: @size,
|
|
45
|
+
attributes: {
|
|
46
|
+
id: @id,
|
|
47
|
+
disabled: @disabled,
|
|
48
|
+
data: {
|
|
49
|
+
orientation: @orientation,
|
|
50
|
+
presentation: @presentation,
|
|
51
|
+
required: @required ? "true" : nil
|
|
52
|
+
}.compact
|
|
53
|
+
}.compact,
|
|
54
|
+
html:,
|
|
55
|
+
aria:,
|
|
56
|
+
data:,
|
|
57
|
+
desperately_need_a_class:
|
|
15
58
|
)
|
|
16
59
|
end
|
|
17
60
|
|
|
18
|
-
attr_reader :name, :
|
|
61
|
+
attr_reader :legend, :options, :name, :value, :description, :id, :size, :orientation, :presentation
|
|
19
62
|
|
|
20
63
|
def view_template
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
64
|
+
fieldset(**root_attributes) do
|
|
65
|
+
html_legend(**slot_attributes(:legend)) { plain(legend) }
|
|
66
|
+
if description
|
|
67
|
+
p(**slot_attributes(:description, attributes: { id: description_id }.compact)) do
|
|
68
|
+
plain(description.to_s)
|
|
69
|
+
end
|
|
26
70
|
end
|
|
27
|
-
end
|
|
28
|
-
end
|
|
29
71
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
render(Label.new(**attrs)) do
|
|
33
|
-
text_or_block(text, &block)
|
|
72
|
+
div(**slot_attributes(:choices)) do
|
|
73
|
+
options.each_with_index { |choice, index| render_choice(choice, index) }
|
|
34
74
|
end
|
|
35
75
|
end
|
|
36
76
|
end
|
|
37
77
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
)
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
78
|
+
private
|
|
79
|
+
|
|
80
|
+
# The fieldset and its legend already scope the group description, so a
|
|
81
|
+
# choice never repeats it through aria-describedby.
|
|
82
|
+
def render_choice(choice, index)
|
|
83
|
+
render_in_slot(
|
|
84
|
+
RadioButton.new(
|
|
85
|
+
label: choice.label,
|
|
86
|
+
description: choice.description,
|
|
87
|
+
id: choice.id || choice_id(index),
|
|
88
|
+
name:,
|
|
89
|
+
value: choice.value,
|
|
90
|
+
checked: selected?(choice),
|
|
91
|
+
disabled: @disabled || choice.disabled,
|
|
92
|
+
required: @required,
|
|
93
|
+
size:
|
|
94
|
+
),
|
|
95
|
+
:choice
|
|
96
|
+
)
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
def selected?(choice)
|
|
100
|
+
!value.nil? && value.to_s == choice.value.to_s
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
def deterministic_id(name)
|
|
104
|
+
derived = name.to_s.gsub(/[^a-zA-Z0-9_-]+/, "_").gsub(/\A_+|_+\z/, "")
|
|
105
|
+
return derived unless derived.empty?
|
|
106
|
+
|
|
107
|
+
raise ArgumentError, "name #{name.inspect} cannot derive an id; pass id:"
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
def choice_id(index)
|
|
111
|
+
"#{id}-#{index}" if id
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
def validate_text!(name, text)
|
|
115
|
+
return text if text.is_a?(String) && !text.strip.empty?
|
|
116
|
+
|
|
117
|
+
raise ArgumentError, "#{name} must be a non-blank String"
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
def validate_unique_choices!
|
|
121
|
+
duplicate_value = options.group_by { |choice| choice.value.to_s }.find { |_value, matches| matches.many? }
|
|
122
|
+
raise ArgumentError, "choice values must be unique" if duplicate_value
|
|
123
|
+
|
|
124
|
+
ids = options.each_with_index.filter_map { |choice, index| choice.id || choice_id(index) }
|
|
125
|
+
raise ArgumentError, "choice ids must be unique" unless ids.uniq.length == ids.length
|
|
55
126
|
end
|
|
56
127
|
end
|
|
57
128
|
end
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module NitroKit
|
|
4
|
+
class ResponsiveValue
|
|
5
|
+
BREAKPOINTS = {
|
|
6
|
+
"sm" => "40rem",
|
|
7
|
+
"md" => "48rem",
|
|
8
|
+
"lg" => "64rem",
|
|
9
|
+
"xl" => "80rem",
|
|
10
|
+
"2xl" => "96rem"
|
|
11
|
+
}.freeze
|
|
12
|
+
|
|
13
|
+
def initialize(property:, value:, allowed:)
|
|
14
|
+
@property = property
|
|
15
|
+
@allowed = allowed.map { |candidate| normalize(candidate) }.freeze
|
|
16
|
+
@values = parse(value).freeze
|
|
17
|
+
freeze
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def to_s
|
|
21
|
+
[ nil, *BREAKPOINTS.keys ].filter_map do |breakpoint|
|
|
22
|
+
value = @values[breakpoint]
|
|
23
|
+
next unless value
|
|
24
|
+
|
|
25
|
+
breakpoint ? "#{breakpoint}:#{value}" : value
|
|
26
|
+
end.join(" ")
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
private
|
|
30
|
+
|
|
31
|
+
def parse(value)
|
|
32
|
+
tokens = tokens_from(value)
|
|
33
|
+
values = {}
|
|
34
|
+
|
|
35
|
+
tokens.each do |token|
|
|
36
|
+
breakpoint, candidate = split(token)
|
|
37
|
+
validate_breakpoint!(breakpoint) if breakpoint
|
|
38
|
+
validate_value!(candidate)
|
|
39
|
+
|
|
40
|
+
if values.key?(breakpoint)
|
|
41
|
+
label = breakpoint ? "breakpoint #{breakpoint.inspect}" : "base value"
|
|
42
|
+
raise ArgumentError, "Duplicate #{@property} #{label}"
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
values[breakpoint] = candidate
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
unless values.key?(nil)
|
|
49
|
+
raise ArgumentError, "#{@property} must include an unprefixed base value"
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
values
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def tokens_from(value)
|
|
56
|
+
tokens = case value
|
|
57
|
+
when String
|
|
58
|
+
value.split
|
|
59
|
+
when Integer, Symbol
|
|
60
|
+
[ normalize(value) ]
|
|
61
|
+
else
|
|
62
|
+
raise ArgumentError,
|
|
63
|
+
"#{@property} must be an Integer, Symbol, or String; received #{value.inspect}"
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
raise ArgumentError, "#{@property} cannot be blank" if tokens.empty?
|
|
67
|
+
|
|
68
|
+
tokens
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def normalize(value)
|
|
72
|
+
value.to_s.tr("_", "-")
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def split(token)
|
|
76
|
+
breakpoint, separator, value = token.partition(":")
|
|
77
|
+
separator.empty? ? [ nil, breakpoint ] : [ breakpoint, value ]
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def validate_breakpoint!(breakpoint)
|
|
81
|
+
return if BREAKPOINTS.key?(breakpoint)
|
|
82
|
+
|
|
83
|
+
raise ArgumentError,
|
|
84
|
+
"Unknown #{@property} breakpoint #{breakpoint.inspect}; expected one of: #{BREAKPOINTS.keys.join(", ")}"
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def validate_value!(value)
|
|
88
|
+
return if @allowed.include?(value)
|
|
89
|
+
|
|
90
|
+
raise ArgumentError,
|
|
91
|
+
"Unknown #{@property} value #{value.inspect}; expected one of: #{@allowed.join(", ")}"
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
end
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module NitroKit
|
|
4
|
+
class RichTextArea < Component
|
|
5
|
+
def initialize(content, id: nil, data: {}, aria: {}, html: {}, desperately_need_a_class: nil)
|
|
6
|
+
@content = validate_content!(content)
|
|
7
|
+
|
|
8
|
+
super(component: :rich_text_area, attributes: { id: }.compact, data:, aria:, html:, desperately_need_a_class:)
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
attr_reader :content
|
|
12
|
+
|
|
13
|
+
def view_template
|
|
14
|
+
div(**root_attributes) do
|
|
15
|
+
div(**slot_attributes(:editor)) { raw(content) }
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
private
|
|
20
|
+
|
|
21
|
+
# The host application's rich-text helper owns the editor markup. Nitro
|
|
22
|
+
# only wraps it, so it must already be trusted output rather than a String
|
|
23
|
+
# this component would have to escape or mark safe on the caller's behalf.
|
|
24
|
+
def validate_content!(content)
|
|
25
|
+
raise ArgumentError, "content is required" if content.nil?
|
|
26
|
+
return content if content.is_a?(ActiveSupport::SafeBuffer) && content.html_safe?
|
|
27
|
+
|
|
28
|
+
raise ArgumentError, "RichTextArea content must be an ActiveSupport::SafeBuffer"
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|