Skip to content

Commit

Permalink
Allow passing :modal option to Button
Browse files Browse the repository at this point in the history
  • Loading branch information
davidalejandroaguilar committed Sep 27, 2024
1 parent 989fc90 commit feba717
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion lib/phlexy_ui/button.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

module PhlexyUI
class Button < Base
def initialize(*, as: :button, **)
def initialize(*, as: :button, modal: nil, **)
super(*, **)
@as = as
@modal = modal
end

def view_template(&)
Expand All @@ -14,12 +15,41 @@ def view_template(&)
base_modifiers:,
options:
).then do |classes|
if modal
script do
unsafe_raw(<<~JS)
(() => {
if (window.PhlexyUI && window.PhlexyUI.modalInitialized) {
return;
}
document.body.addEventListener("click", (event) => {
if (event.target.tagName === 'BUTTON' &&
event.target.dataset.modal) {
const modal = document.getElementById(event.target.dataset.modal);
if (modal) {
modal.showModal();
}
}
});
if (!window.PhlexyUI) window.PhlexyUI = {};
window.PhlexyUI.modalInitialized = true;
})();
JS
end
options[:data] ||= {}
options[:data][:modal] = modal
end

public_send(as, class: classes, **options, &)
end
end

private

attr_reader :modal

register_modifiers(
# Modifiers
# "sm:no-animation"
Expand Down

0 comments on commit feba717

Please sign in to comment.