whose-components 0.3.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 (104) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.md +231 -0
  4. data/Rakefile +8 -0
  5. data/app/assets/fonts/whose_components/OFL.txt +93 -0
  6. data/app/assets/fonts/whose_components/leaguespartan-bold.woff2 +0 -0
  7. data/app/assets/tailwind/whose_components/engine.css +139 -0
  8. data/app/components/whose/badge_component/badge_component.html.haml +1 -0
  9. data/app/components/whose/badge_component.rb +23 -0
  10. data/app/components/whose/button_component/button_component.html.haml +6 -0
  11. data/app/components/whose/button_component.rb +40 -0
  12. data/app/components/whose/card_component/card_component.html.haml +6 -0
  13. data/app/components/whose/card_component.rb +24 -0
  14. data/app/components/whose/dialog.rb +21 -0
  15. data/app/components/whose/dialog_header_component/dialog_header_component.en.yml +2 -0
  16. data/app/components/whose/dialog_header_component/dialog_header_component.html.haml +3 -0
  17. data/app/components/whose/dialog_header_component.rb +8 -0
  18. data/app/components/whose/flash_component/flash_component.en.yml +2 -0
  19. data/app/components/whose/flash_component/flash_component.html.haml +14 -0
  20. data/app/components/whose/flash_component.rb +35 -0
  21. data/app/components/whose/form/field_component/field_component.html.haml +16 -0
  22. data/app/components/whose/form/field_component.rb +90 -0
  23. data/app/components/whose/link_component/link_component.html.haml +1 -0
  24. data/app/components/whose/link_component.rb +22 -0
  25. data/app/components/whose/list_component/list_component.html.haml +3 -0
  26. data/app/components/whose/list_component.rb +15 -0
  27. data/app/components/whose/list_row_component/list_row_component.html.haml +12 -0
  28. data/app/components/whose/list_row_component.rb +19 -0
  29. data/app/components/whose/modal_component/modal_component.html.haml +10 -0
  30. data/app/components/whose/modal_component.rb +12 -0
  31. data/app/components/whose/nav/item_component/item_component.html.haml +5 -0
  32. data/app/components/whose/nav/item_component.rb +21 -0
  33. data/app/components/whose/nav_component/nav_component.en.yml +4 -0
  34. data/app/components/whose/nav_component/nav_component.html.haml +22 -0
  35. data/app/components/whose/nav_component.rb +55 -0
  36. data/app/components/whose/offcanvas_component/offcanvas_component.html.haml +8 -0
  37. data/app/components/whose/offcanvas_component.rb +21 -0
  38. data/app/components/whose/page_header_component/page_header_component.html.haml +7 -0
  39. data/app/components/whose/page_header_component.rb +17 -0
  40. data/app/components/whose/pagination_component/pagination_component.html.haml +2 -0
  41. data/app/components/whose/pagination_component.rb +12 -0
  42. data/app/components/whose/tooltip_component/tooltip_component.html.haml +3 -0
  43. data/app/components/whose/tooltip_component.rb +26 -0
  44. data/app/controllers/concerns/whose/authentication.rb +77 -0
  45. data/app/controllers/whose/confirmations_controller.rb +29 -0
  46. data/app/controllers/whose/passwords_controller.rb +38 -0
  47. data/app/controllers/whose/registrations_controller.rb +29 -0
  48. data/app/controllers/whose/sessions_controller.rb +31 -0
  49. data/app/helpers/whose/component_helper.rb +18 -0
  50. data/app/helpers/whose/form_helper.rb +16 -0
  51. data/app/javascript/whose/controllers/whose_dialog_controller.js +3 -0
  52. data/app/javascript/whose/controllers/whose_notification_controller.js +8 -0
  53. data/app/javascript/whose/controllers/whose_password_visibility_controller.js +3 -0
  54. data/app/jobs/.keep +0 -0
  55. data/app/mailers/whose/auth_mailer.rb +17 -0
  56. data/app/models/concerns/whose/authenticatable.rb +31 -0
  57. data/app/models/whose/current.rb +6 -0
  58. data/app/models/whose/session.rb +7 -0
  59. data/app/views/whose/auth_mailer/account_exists.html.haml +6 -0
  60. data/app/views/whose/auth_mailer/account_exists.text.haml +9 -0
  61. data/app/views/whose/auth_mailer/confirmation.html.haml +3 -0
  62. data/app/views/whose/auth_mailer/confirmation.text.haml +5 -0
  63. data/app/views/whose/auth_mailer/password_reset.html.haml +3 -0
  64. data/app/views/whose/auth_mailer/password_reset.text.haml +5 -0
  65. data/app/views/whose/confirmations/new.html.haml +11 -0
  66. data/app/views/whose/confirmations/show.html.haml +6 -0
  67. data/app/views/whose/passwords/edit.html.haml +11 -0
  68. data/app/views/whose/passwords/new.html.haml +11 -0
  69. data/app/views/whose/registrations/new.html.haml +16 -0
  70. data/app/views/whose/sessions/new.html.haml +15 -0
  71. data/config/importmap.rb +7 -0
  72. data/config/locales/whose/auth.en.yml +71 -0
  73. data/config/locales/whose/errors.en.yml +17 -0
  74. data/config/locales/whose/form.en.yml +5 -0
  75. data/config/locales/whose/nav.en.yml +4 -0
  76. data/lib/generators/whose/components/install/install_generator.rb +56 -0
  77. data/lib/generators/whose/components/install/templates/application.html.haml +24 -0
  78. data/lib/generators/whose/components/install/templates/create_users.rb.tt +17 -0
  79. data/lib/generators/whose/components/install/templates/create_whose_sessions.rb.tt +13 -0
  80. data/lib/generators/whose/components/install/templates/user.rb.tt +7 -0
  81. data/lib/generators/whose/components/install/templates/users.rb.tt +15 -0
  82. data/lib/generators/whose/components/views/USAGE +12 -0
  83. data/lib/generators/whose/components/views/views_generator.rb +19 -0
  84. data/lib/templates/app/.claude/skills/asd-ste100/LICENSE +21 -0
  85. data/lib/templates/app/.claude/skills/asd-ste100/README.md +102 -0
  86. data/lib/templates/app/.claude/skills/asd-ste100/SKILL.md +139 -0
  87. data/lib/templates/app/.claude/skills/asd-ste100/examples/before-after.md +92 -0
  88. data/lib/templates/app/.claude/skills/asd-ste100/examples/linter-edge-cases.md +14 -0
  89. data/lib/templates/app/.claude/skills/asd-ste100/references/writing-rules.md +59 -0
  90. data/lib/templates/app/.claude/skills/asd-ste100/scripts/ste-lint.py +486 -0
  91. data/lib/templates/app/CLAUDE.md.tt +181 -0
  92. data/lib/templates/whose_app_template.rb +144 -0
  93. data/lib/whose/components/engine.rb +33 -0
  94. data/lib/whose/components/form_builder.rb +158 -0
  95. data/lib/whose/components/routes.rb +16 -0
  96. data/lib/whose/components/version.rb +7 -0
  97. data/lib/whose/components.rb +16 -0
  98. data/lib/whose/merge_classes.rb +17 -0
  99. data/vendor/javascript/@stimulus-components--dialog.js +4 -0
  100. data/vendor/javascript/@stimulus-components--notification.js +4 -0
  101. data/vendor/javascript/@stimulus-components--password-visibility.js +4 -0
  102. data/vendor/javascript/LICENSES +30 -0
  103. data/vendor/javascript/stimulus-use.js +4 -0
  104. metadata +272 -0
@@ -0,0 +1,14 @@
1
+ %div{ attributes }
2
+ - @messages.each do |type, message|
3
+ %div{ class: "flex items-start gap-3 rounded-box border p-4 font-sans text-sm shadow-lg #{style_for(type)}",
4
+ role: type == "alert" ? "alert" : "status",
5
+ data: { controller: "whose-notification",
6
+ whose_notification_delay_value: delay_for(type),
7
+ transition_enter_active: "transition ease-out duration-300",
8
+ transition_enter_from: "opacity-0",
9
+ transition_enter_to: "opacity-100",
10
+ transition_leave_active: "transition ease-in duration-200",
11
+ transition_leave_from: "opacity-100",
12
+ transition_leave_to: "opacity-0" } }
13
+ %p.flex-1= message
14
+ %button.shrink-0.cursor-pointer.rounded-box.px-1.opacity-70.hover:opacity-100.focus-visible:outline-2.focus-visible:outline-offset-2.focus-visible:outline-accent{ type: "button", aria: { label: t(".close") }, data: { action: "whose-notification#hide" } } ×
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Whose::FlashComponent < ViewComponent::Base
4
+ STYLES = {
5
+ "notice" => "bg-positive-soft text-positive border-positive",
6
+ "alert" => "bg-negative-soft text-negative border-negative"
7
+ }.freeze
8
+ NEUTRAL_STYLE = "bg-surface text-fg border-border"
9
+ AUTO_HIDE_DELAY = 5000
10
+
11
+ BASE_CLASSES = "fixed top-4 right-4 z-50 flex w-full max-w-sm flex-col gap-2 px-4 sm:px-0"
12
+
13
+ def initialize(flash:, **html_attributes)
14
+ @messages = flash.to_h.transform_keys(&:to_s)
15
+ @html_attributes = html_attributes
16
+ end
17
+
18
+ def render?
19
+ @messages.any?
20
+ end
21
+
22
+ def attributes
23
+ @html_attributes.merge(class: Whose.merge_classes(BASE_CLASSES, @html_attributes[:class]))
24
+ end
25
+
26
+ private
27
+
28
+ def style_for(type)
29
+ STYLES.fetch(type, NEUTRAL_STYLE)
30
+ end
31
+
32
+ def delay_for(type)
33
+ type == "alert" ? 0 : AUTO_HIDE_DELAY
34
+ end
35
+ end
@@ -0,0 +1,16 @@
1
+ %div{ wrapper_attributes }
2
+ - if @group
3
+ %span{id: label_id, class: LABEL_CLASSES[:vertical]}= label_text
4
+ - elsif !@inline
5
+ = @form.label(@method, label_text, class: label_classes)
6
+ %div{class: "flex flex-col gap-1 sm:col-start-2"}
7
+ - if @inline
8
+ .flex.items-center.gap-2
9
+ = content
10
+ = @form.label(@method, label_text, class: "text-sm text-fg")
11
+ - else
12
+ = content
13
+ - if @hint
14
+ %p{id: hint_id, class: "text-sm text-muted"}= @hint
15
+ - if error_message
16
+ %p{id: error_id, class: "text-sm text-negative"}= error_message
@@ -0,0 +1,90 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Whose::Form::FieldComponent < ViewComponent::Base
4
+ CLASSES = {
5
+ input: "block w-full rounded-box border border-border bg-surface px-3 py-2 text-fg font-sans " \
6
+ "placeholder:text-muted aria-invalid:border-negative " \
7
+ "focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-accent",
8
+ textarea: "min-h-24 field-sizing-content",
9
+ file: "file:mr-3 file:rounded-box file:border-0 file:bg-accent-soft file:px-3 file:py-1 file:text-accent-text",
10
+ check: "size-4 rounded-box border border-border accent-accent " \
11
+ "focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-accent",
12
+ color: "h-10 cursor-pointer p-1!",
13
+ radio: "size-4 rounded-full border border-border accent-accent " \
14
+ "focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-accent",
15
+ toggle: "h-5 w-9 shrink-0 cursor-pointer appearance-none rounded-full bg-border transition-colors checked:bg-accent " \
16
+ "before:m-0.5 before:block before:size-4 before:rounded-full before:bg-surface before:transition-transform " \
17
+ "checked:before:translate-x-4 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-accent",
18
+ select_wrapper: "grid",
19
+ select: "col-start-1 row-start-1 appearance-none pr-10",
20
+ select_caret: "pointer-events-none col-start-1 row-start-1 mr-2.5 size-5 self-center justify-self-end text-fg",
21
+ password_wrapper: "relative",
22
+ password_input: "pr-16",
23
+ password_hidden: "hidden",
24
+ password_button: "absolute inset-y-0 right-0 px-3 text-sm text-accent-text cursor-pointer " \
25
+ "focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-accent"
26
+ }.freeze
27
+ WRAPPER_CLASSES = {
28
+ vertical: "flex flex-col gap-1",
29
+ horizontal: "flex flex-col gap-1 sm:grid sm:grid-cols-[12rem_1fr] sm:gap-x-4",
30
+ inline: "flex flex-col gap-1 sm:flex-1"
31
+ }.freeze
32
+ LABEL_CLASSES = {
33
+ vertical: "text-sm font-medium text-fg",
34
+ horizontal: "text-sm font-medium text-fg sm:pt-2",
35
+ inline: "text-sm font-medium text-fg"
36
+ }.freeze
37
+
38
+ def initialize(form:, method:, label:, hint:, layout:, inline: false, group: false, placeholder_label: false)
39
+ @group = group
40
+ @form = form
41
+ @method = method
42
+ @label = label
43
+ @hint = hint
44
+ @layout = layout
45
+ @inline = inline
46
+ @placeholder_label = placeholder_label
47
+ end
48
+
49
+ def placeholder_label?
50
+ @placeholder_label
51
+ end
52
+
53
+ def label_text
54
+ @label || @method.to_s.humanize
55
+ end
56
+
57
+ def error_message
58
+ @form.object&.errors&.[](@method)&.first
59
+ end
60
+
61
+ def error_id
62
+ "#{@form.object_name}_#{@method}_error"
63
+ end
64
+
65
+ def hint_id
66
+ "#{@form.object_name}_#{@method}_hint"
67
+ end
68
+
69
+ def label_id
70
+ "#{@form.object_name}_#{@method}_label"
71
+ end
72
+
73
+ def aria(caller_aria = {})
74
+ describedby = [ caller_aria[:describedby], (hint_id if @hint), (error_id if error_message) ].compact.join(" ")
75
+ caller_aria.merge(invalid: (true if error_message), describedby: describedby.presence).compact
76
+ end
77
+
78
+ def wrapper_attributes
79
+ attributes = { class: wrapper_classes, data: { field: true, layout: @layout } }
80
+ @group ? attributes.merge(role: "group", aria: { labelledby: label_id }) : attributes
81
+ end
82
+
83
+ def wrapper_classes
84
+ placeholder_label? && @layout == :horizontal ? WRAPPER_CLASSES[:vertical] : WRAPPER_CLASSES[@layout]
85
+ end
86
+
87
+ def label_classes
88
+ placeholder_label? ? "sr-only" : LABEL_CLASSES[@layout]
89
+ end
90
+ end
@@ -0,0 +1 @@
1
+ = link_to content, @href, attributes
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Whose::LinkComponent < ViewComponent::Base
4
+ BASE_CLASSES = "rounded-box hover:underline " \
5
+ "focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-accent"
6
+ VARIANT_CLASSES = {
7
+ accent: "text-accent-text",
8
+ muted: "text-muted hover:text-fg"
9
+ }.freeze
10
+
11
+ def initialize(href:, variant: :accent, **html_attributes)
12
+ @variant = variant.to_sym
13
+ raise ArgumentError, "Unknown variant: #{variant.inspect}." unless VARIANT_CLASSES.key?(@variant)
14
+
15
+ @href = href
16
+ @html_attributes = html_attributes
17
+ end
18
+
19
+ def attributes
20
+ @html_attributes.merge(class: Whose.merge_classes(BASE_CLASSES, VARIANT_CLASSES[@variant], @html_attributes[:class]))
21
+ end
22
+ end
@@ -0,0 +1,3 @@
1
+ %ul{ attributes }
2
+ - rows.each do |row|
3
+ = row
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Whose::ListComponent < ViewComponent::Base
4
+ renders_many :rows, Whose::ListRowComponent
5
+
6
+ BASE_CLASSES = "w-full divide-y divide-border overflow-hidden rounded-box border border-border bg-surface font-sans"
7
+
8
+ def initialize(**html_attributes)
9
+ @html_attributes = html_attributes
10
+ end
11
+
12
+ def attributes
13
+ @html_attributes.merge(class: Whose.merge_classes(BASE_CLASSES, @html_attributes[:class]))
14
+ end
15
+ end
@@ -0,0 +1,12 @@
1
+ %li{attributes}
2
+ - if leading?
3
+ %div.shrink-0{ data: { slot: "leading" } }= leading
4
+ %div.min-w-0.flex-1
5
+ - if @href
6
+ = link_to @title, @href, class: "block truncate rounded-box text-sm font-medium text-fg hover:text-accent-text focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-accent"
7
+ - else
8
+ %p.truncate.text-sm.font-medium.text-fg= @title
9
+ - if @subtitle
10
+ %p.truncate.text-sm.text-muted= @subtitle
11
+ - if trailing?
12
+ %div.shrink-0.text-sm.text-muted{ data: { slot: "trailing" } }= trailing
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Whose::ListRowComponent < ViewComponent::Base
4
+ renders_one :leading
5
+ renders_one :trailing
6
+
7
+ BASE_CLASSES = "flex items-center gap-3 px-4 py-3 font-sans"
8
+
9
+ def initialize(title:, subtitle: nil, href: nil, **html_attributes)
10
+ @title = title
11
+ @subtitle = subtitle
12
+ @href = href
13
+ @html_attributes = html_attributes
14
+ end
15
+
16
+ def attributes
17
+ @html_attributes.merge(class: Whose.merge_classes(BASE_CLASSES, @html_attributes[:class]))
18
+ end
19
+ end
@@ -0,0 +1,10 @@
1
+ %div{ attributes }
2
+ %span.contents{ data: { action: "click->whose-dialog#open" } }= trigger
3
+ %dialog{ class: "m-auto w-full max-w-lg rounded-box border border-border bg-surface p-0 font-sans text-fg shadow-xl backdrop:bg-backdrop",
4
+ aria: { labelledby: title_id },
5
+ data: { whose_dialog_target: "dialog" } }
6
+ .flex.flex-col.gap-4.p-6
7
+ = render Whose::DialogHeaderComponent.new(title: @title, title_id:)
8
+ .text-sm= content
9
+ - if footer?
10
+ .flex.justify-end.gap-2.border-t.border-border.pt-4= footer
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Whose::ModalComponent < ViewComponent::Base
4
+ include Whose::Dialog
5
+
6
+ renders_one :footer
7
+
8
+ def initialize(title:, **html_attributes)
9
+ @title = title
10
+ @html_attributes = html_attributes
11
+ end
12
+ end
@@ -0,0 +1,5 @@
1
+ %li{ @html_attributes }
2
+ = link_to @href, class: "flex items-center gap-2 rounded-box px-3 py-2 text-sm font-medium focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-accent #{link_style}", aria: { current: ("page" if active?) } do
3
+ - if @icon
4
+ %span.shrink-0= @icon
5
+ %span= @label
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Whose::Nav::ItemComponent < ViewComponent::Base
4
+ def initialize(label:, href:, icon: nil, active: nil, **html_attributes)
5
+ @label = label
6
+ @href = href
7
+ @icon = icon
8
+ @active = active
9
+ @html_attributes = html_attributes
10
+ end
11
+
12
+ def active?
13
+ return @active unless @active.nil?
14
+
15
+ current_page?(@href) || request.path.start_with?("#{@href}/")
16
+ end
17
+
18
+ def link_style
19
+ active? ? "bg-accent-soft text-accent-text" : "text-muted hover:text-fg"
20
+ end
21
+ end
@@ -0,0 +1,4 @@
1
+ en:
2
+ main: Main
3
+ menu: Menu
4
+ open_menu: Open menu
@@ -0,0 +1,22 @@
1
+ %nav{ attributes }
2
+ - if brand? && menu?
3
+ .shrink-0.font-semibold{ data: { slot: "brand" } }= brand
4
+ %ul{ class: style[:list] }
5
+ - items.each do |item|
6
+ = item
7
+ - if actions? && menu?
8
+ %div{ class: style[:actions], data: { slot: "actions" } }= actions
9
+ - if menu?
10
+ = render Whose::OffcanvasComponent.new(title: t(".menu")) do |offcanvas|
11
+ - offcanvas.with_trigger do
12
+ %button.cursor-pointer.rounded-box.p-2.hover:bg-accent-soft.focus-visible:outline-2.focus-visible:outline-offset-2.focus-visible:outline-accent.md:hidden{ type: "button", aria: { label: t(".open_menu") } }
13
+ %svg.size-5{ xmlns: "http://www.w3.org/2000/svg", fill: "none", viewBox: "0 0 24 24", stroke: "currentColor", stroke_width: "2", aria: { hidden: "true" } }
14
+ %path{ stroke_linecap: "round", stroke_linejoin: "round", d: "M4 6h16M4 12h16M4 18h16" }
15
+ .flex.flex-col.gap-4
16
+ %ul.flex.flex-col.gap-1
17
+ - items.each do |item|
18
+ = item
19
+ - if actions?
20
+ .flex.items-center.gap-2= actions
21
+ - if spacer?
22
+ %div{ class: spacer_style, aria: { hidden: "true" }, data: { slot: "spacer" } }
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Whose::NavComponent < ViewComponent::Base
4
+ BAR_STYLE = "flex items-center justify-between gap-4 border-border bg-surface font-sans text-fg"
5
+ BOTTOM_HEIGHT = "h-[calc(3.5rem+env(safe-area-inset-bottom))]"
6
+ ORIENTATION_STYLES = {
7
+ horizontal: {
8
+ root: "#{BAR_STYLE} w-full border-b px-4 py-3",
9
+ list: "hidden flex-1 items-center gap-1 md:flex",
10
+ actions: "hidden items-center gap-2 md:flex"
11
+ },
12
+ vertical: {
13
+ root: "#{BAR_STYLE} w-full border-b px-4 py-3 md:h-dvh md:w-60 md:flex-col md:items-stretch md:justify-start md:border-r md:border-b-0 md:p-4",
14
+ list: "hidden flex-col gap-1 md:flex",
15
+ actions: "hidden items-center gap-2 md:mt-auto md:flex"
16
+ },
17
+ bottom: {
18
+ root: "#{BAR_STYLE} #{BOTTOM_HEIGHT} fixed inset-x-0 bottom-0 z-10 border-t px-2 pb-[env(safe-area-inset-bottom)]",
19
+ list: "flex flex-1 justify-center gap-4 [&_a]:flex-col [&_a]:gap-0.5 [&_a]:text-xs"
20
+ }
21
+ }.freeze
22
+
23
+ renders_one :brand
24
+ renders_one :actions
25
+ renders_many :items, Whose::Nav::ItemComponent
26
+
27
+ def initialize(orientation: :horizontal, **html_attributes)
28
+ @html_attributes = html_attributes
29
+ @orientation = orientation.to_sym
30
+ raise ArgumentError, "Unknown orientation: #{orientation}. Use :horizontal, :vertical or :bottom." unless ORIENTATION_STYLES.key?(@orientation)
31
+ end
32
+
33
+ def attributes
34
+ @html_attributes.merge(
35
+ class: Whose.merge_classes(style[:root], @html_attributes[:class]),
36
+ aria: { label: t(".main") }.merge(@html_attributes[:aria] || {})
37
+ )
38
+ end
39
+
40
+ def spacer_style
41
+ BOTTOM_HEIGHT
42
+ end
43
+
44
+ def spacer?
45
+ @orientation == :bottom
46
+ end
47
+
48
+ def menu?
49
+ @orientation != :bottom
50
+ end
51
+
52
+ def style
53
+ ORIENTATION_STYLES.fetch(@orientation)
54
+ end
55
+ end
@@ -0,0 +1,8 @@
1
+ %div{ attributes }
2
+ %span.contents{ data: { action: "click->whose-dialog#open" } }= trigger
3
+ %dialog{ class: "m-0 h-dvh max-h-none w-80 max-w-[85vw] border-border bg-surface p-0 font-sans text-fg shadow-xl backdrop:bg-backdrop #{side_style}",
4
+ aria: { labelledby: title_id },
5
+ data: { side: @side, whose_dialog_target: "dialog" } }
6
+ .flex.h-full.flex-col.gap-4.overflow-y-auto.p-6
7
+ = render Whose::DialogHeaderComponent.new(title: @title, title_id:)
8
+ .text-sm= content
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Whose::OffcanvasComponent < ViewComponent::Base
4
+ SIDE_STYLES = {
5
+ left: "mr-auto",
6
+ right: "ml-auto"
7
+ }.freeze
8
+
9
+ include Whose::Dialog
10
+
11
+ def initialize(title:, side: :left, **html_attributes)
12
+ @title = title
13
+ @html_attributes = html_attributes
14
+ @side = side.to_sym
15
+ raise ArgumentError, "Unknown side: #{side}. Use :left or :right." unless SIDE_STYLES.key?(@side)
16
+ end
17
+
18
+ def side_style
19
+ SIDE_STYLES.fetch(@side)
20
+ end
21
+ end
@@ -0,0 +1,7 @@
1
+ %header{ attributes }
2
+ %div
3
+ %h1.text-2xl.font-semibold.text-fg= @title
4
+ - if @subtitle
5
+ %p.mt-1.text-sm.text-muted= @subtitle
6
+ - if actions?
7
+ %div.flex.items-center.gap-2{ data: { slot: "actions" } }= actions
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Whose::PageHeaderComponent < ViewComponent::Base
4
+ renders_one :actions
5
+
6
+ BASE_CLASSES = "flex w-full flex-wrap items-center justify-between gap-4 font-sans"
7
+
8
+ def initialize(title:, subtitle: nil, **html_attributes)
9
+ @title = title
10
+ @subtitle = subtitle
11
+ @html_attributes = html_attributes
12
+ end
13
+
14
+ def attributes
15
+ @html_attributes.merge(class: Whose.merge_classes(BASE_CLASSES, @html_attributes[:class]))
16
+ end
17
+ end
@@ -0,0 +1,2 @@
1
+ %div{ @html_attributes }
2
+ = @pagy.series_nav.html_safe
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Whose::PaginationComponent < ViewComponent::Base
4
+ def initialize(pagy:, **html_attributes)
5
+ @pagy = pagy
6
+ @html_attributes = html_attributes
7
+ end
8
+
9
+ def render?
10
+ @pagy.last > 1
11
+ end
12
+ end
@@ -0,0 +1,3 @@
1
+ %span{ attributes }
2
+ = content
3
+ %span.invisible.absolute.z-10.w-max.max-w-xs.rounded-box.bg-fg.px-2.py-1.text-xs.text-canvas.opacity-0.transition-opacity{ id:, role: "tooltip", class: [placement_classes, "group-hover:visible group-hover:opacity-100 group-focus-within:visible group-focus-within:opacity-100"] }= text
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ # The gap uses margin, so a pointer that moves onto the tooltip hides it.
4
+ class Whose::TooltipComponent < ViewComponent::Base
5
+ PLACEMENTS = {
6
+ top: "bottom-full left-1/2 -translate-x-1/2 mb-2",
7
+ bottom: "top-full left-1/2 -translate-x-1/2 mt-2",
8
+ left: "right-full top-1/2 -translate-y-1/2 mr-2",
9
+ right: "left-full top-1/2 -translate-y-1/2 ml-2"
10
+ }.freeze
11
+
12
+ attr_reader :text, :id, :placement_classes
13
+
14
+ BASE_CLASSES = "group relative inline-flex w-fit"
15
+
16
+ def initialize(text:, placement: :top, **html_attributes)
17
+ @text = text
18
+ @html_attributes = html_attributes
19
+ @placement_classes = PLACEMENTS.fetch(placement.to_sym) { raise ArgumentError, "Unknown placement: #{placement.inspect}" }
20
+ @id = "whose-tooltip-#{SecureRandom.hex(4)}"
21
+ end
22
+
23
+ def attributes
24
+ @html_attributes.merge(class: Whose.merge_classes(BASE_CLASSES, @html_attributes[:class]))
25
+ end
26
+ end
@@ -0,0 +1,77 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Whose::Authentication
4
+ extend ActiveSupport::Concern
5
+
6
+ included do
7
+ before_action :require_authentication
8
+ helper_method :authenticated?, :current_user
9
+ end
10
+
11
+ class_methods do
12
+ def allow_unauthenticated_access(**options)
13
+ skip_before_action :require_authentication, **options
14
+ end
15
+ end
16
+
17
+ private
18
+
19
+ def authenticated?
20
+ resume_session.present?
21
+ end
22
+
23
+ def current_user
24
+ resume_session&.user
25
+ end
26
+
27
+ def after_sign_in_path
28
+ main_app.respond_to?(:root_path) ? main_app.root_path : "/"
29
+ end
30
+
31
+ def after_sign_out_path
32
+ new_session_path
33
+ end
34
+
35
+ def after_sign_up_path
36
+ new_session_path
37
+ end
38
+
39
+ def sign_up_params
40
+ params.expect(user: %i[first_name last_name email password password_confirmation])
41
+ end
42
+
43
+ def after_sign_up(user); end
44
+
45
+ def require_authentication
46
+ resume_session || request_authentication
47
+ end
48
+
49
+ def resume_session
50
+ Whose::Current.session ||= find_session_by_cookie
51
+ end
52
+
53
+ def find_session_by_cookie
54
+ Whose::Session.find_by(id: cookies.signed[:session_id]) if cookies.signed[:session_id]
55
+ end
56
+
57
+ def request_authentication
58
+ session[:return_to_after_authenticating] = request.url
59
+ redirect_to new_session_path
60
+ end
61
+
62
+ def after_authentication_url
63
+ session.delete(:return_to_after_authenticating) || after_sign_in_path
64
+ end
65
+
66
+ def start_new_session_for(user)
67
+ user.sessions.create!(user_agent: request.user_agent, ip_address: request.remote_ip).tap do |session|
68
+ Whose::Current.session = session
69
+ cookies.signed.permanent[:session_id] = { value: session.id, httponly: true, same_site: :lax }
70
+ end
71
+ end
72
+
73
+ def terminate_session
74
+ Whose::Current.session.destroy
75
+ cookies.delete(:session_id)
76
+ end
77
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Whose::ConfirmationsController < ::ApplicationController
4
+ include Whose::Authentication
5
+
6
+ allow_unauthenticated_access only: %i[new create show update]
7
+ rate_limit to: 10, within: 3.minutes, only: :create,
8
+ with: -> { redirect_to new_confirmation_path, alert: t("whose.rate_limited") }
9
+
10
+ def new; end
11
+
12
+ def create
13
+ user = User.find_by(email: params.expect(:email))
14
+ Whose::AuthMailer.confirmation(user).deliver_later if user && !user.confirmed?
15
+ redirect_to new_session_path, notice: t("whose.confirmations.sent")
16
+ end
17
+
18
+ def show; end
19
+
20
+ def update
21
+ if (user = User.find_by_token_for(:email_confirmation, params[:token]))
22
+ user.confirm!
23
+ start_new_session_for user
24
+ redirect_to after_sign_in_path, notice: t("whose.confirmations.confirmed")
25
+ else
26
+ redirect_to new_confirmation_path, alert: t("whose.confirmations.invalid")
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Whose::PasswordsController < ::ApplicationController
4
+ include Whose::Authentication
5
+
6
+ allow_unauthenticated_access
7
+ rate_limit to: 10, within: 3.minutes, only: :create,
8
+ with: -> { redirect_to new_password_path, alert: t("whose.rate_limited") }
9
+ before_action :set_user_by_token, only: %i[edit update]
10
+
11
+ def new; end
12
+
13
+ def create
14
+ user = User.find_by(email: params.expect(:email))
15
+ Whose::AuthMailer.password_reset(user).deliver_later if user
16
+ redirect_to new_session_path, notice: t("whose.passwords.sent")
17
+ end
18
+
19
+ def edit; end
20
+
21
+ def update
22
+ attributes = params.expect(user: %i[password password_confirmation])
23
+ if attributes[:password].present? && User.transaction { @user.update(attributes) && @user.sessions.destroy_all }
24
+ redirect_to new_session_path, notice: t("whose.passwords.updated")
25
+ else
26
+ @user.errors.add(:password, :blank) if attributes[:password].blank?
27
+ render :edit, status: :unprocessable_content
28
+ end
29
+ end
30
+
31
+ private
32
+
33
+ def set_user_by_token
34
+ @user = User.find_by_password_reset_token!(params[:token])
35
+ rescue ActiveSupport::MessageVerifier::InvalidSignature, ActiveRecord::RecordNotFound
36
+ redirect_to new_password_path, alert: t("whose.passwords.invalid")
37
+ end
38
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Whose::RegistrationsController < ::ApplicationController
4
+ include Whose::Authentication
5
+
6
+ allow_unauthenticated_access only: %i[new create]
7
+ rate_limit to: 10, within: 3.minutes, only: :create,
8
+ with: -> { redirect_to new_registration_path, alert: t("whose.rate_limited") }
9
+ before_action -> { redirect_to after_sign_in_path if authenticated? }
10
+
11
+ def new
12
+ @user = User.new
13
+ end
14
+
15
+ def create
16
+ @user = User.new(sign_up_params)
17
+ if @user.save
18
+ after_sign_up(@user)
19
+ Whose::AuthMailer.confirmation(@user).deliver_later
20
+ else
21
+ @user.errors.delete(:email, :taken)
22
+ return render :new, status: :unprocessable_content if @user.errors.any?
23
+
24
+ owner = User.find_by!(email: @user.email)
25
+ (owner.confirmed? ? Whose::AuthMailer.account_exists(owner) : Whose::AuthMailer.confirmation(owner)).deliver_later
26
+ end
27
+ redirect_to after_sign_up_path, notice: t("whose.registrations.created")
28
+ end
29
+ end