Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The modal helper now accepts an animated: option #153

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions examples/middleman/source/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
<!-- Try setting different :title, :size, :button or :context options -->
<%= modal 'Do what you want!', button: {size: :small, context: :info} %>

<% modal size: :large, id: 'modal', button: {class: :en} do %>
<% modal size: :large, id: 'modal', button: {class: :en}, animated: false do %>
<div class="modal-body">Please accept the Terms of service.</div>
<div class="modal-footer"><button type="button" class="btn btn-primary">Accept</button></div>
<% end %>
Expand Down Expand Up @@ -155,4 +155,4 @@
<%= javascript_include_tag '//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js' %>
<%= javascript_include_tag bootstrap_js %>
</body>
</html>
</html>
4 changes: 2 additions & 2 deletions examples/padrino/app/views/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
<!-- Try setting different :title, :size, :button or :context options -->
<%= modal 'Do what you want!', button: {size: :small, context: :info} %>

<% modal title: 'Terms of service', size: :large, id: 'modal', button: {class: :en} do %>
<% modal title: 'Terms of service', size: :large, id: 'modal', button: {class: :en}, animated: false do %>
<div class="modal-body">Please accept the Terms of service.</div>
<div class="modal-footer"><button type="button" class="btn btn-primary">Accept</button></div>
<% end %>
Expand Down Expand Up @@ -152,4 +152,4 @@
<%= javascript_include_tag '//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js' %>
<%= javascript_include_tag bootstrap_js %>
</body>
</html>
</html>
4 changes: 2 additions & 2 deletions examples/rails/app/views/application/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
<!-- Try setting different :title, :size, :button or :context options -->
<%= modal 'Do what you want!', button: {size: :small, context: :info} %>

<%= modal title: 'Terms of service', size: :large, id: 'modal', button: {class: :en} do %>
<%= modal title: 'Terms of service', size: :large, id: 'modal', button: {class: :en}, animated: false do %>
<div class="modal-body">Please accept the Terms of service.</div>
<div class="modal-footer"><button type="button" class="btn btn-primary">Accept</button></div>
<% end %>
Expand Down Expand Up @@ -157,4 +157,4 @@
<%= javascript_include_tag '//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js' %>
<%= javascript_include_tag bootstrap_js %>
</body>
</html>
</html>
12 changes: 11 additions & 1 deletion lib/bh/classes/modal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ def dialog_size_class
Modal.dialog_sizes[@options[:size]]
end

# @return [#to_s] the animation-related class to assign to the modal.
def animation_class
Modal.animation_class if @options.fetch :animated, true
end

# @return [#to_s] the caption for the modal button.
def caption
@options.fetch(:button, {}).fetch :caption, title
Expand Down Expand Up @@ -56,6 +61,11 @@ def self.dialog_sizes
end
end

# @return [#to_s] the class that defines the modal's animation.
def self.animation_class
'fade'
end

def extract_content_from(*args, &block)
if block_given?
super
Expand All @@ -65,4 +75,4 @@ def extract_content_from(*args, &block)
end
end
end
end
end
8 changes: 6 additions & 2 deletions lib/bh/helpers/modal_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ module Helpers
# (alias `:xs`), `:large` (alias `:lg`) or `:small` (alias `:sm`).
# * :layout (#to_s) if set to `:block`, span the button for the full
# width of the parent.
# @option options [Boolean] :animated (true) option for disabling the
# default modal animation.
# @example Display a simple modal toggled by a blue button.
# modal 'You clicked me!', title: 'Click me', button: {context: :info}
# @overload modal(options = {}, &block)
Expand All @@ -37,7 +39,9 @@ module Helpers
# end
def modal(*args, &block)
modal = Bh::Modal.new self, *args, &block
modal.extract! :button, :size, :body, :title, :id
modal.extract! :animated, :button, :size, :body, :title, :id

modal.append_class_to! :animation, modal.animation_class

modal.extract_from :button, [:context, :size, :layout, :caption]
modal.append_class_to! :button, :btn
Expand All @@ -52,4 +56,4 @@ def modal(*args, &block)
modal.render_partial 'modal'
end
end
end
end
4 changes: 2 additions & 2 deletions lib/bh/views/bh/_modal.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<button class="<%= button[:class] %>" data-toggle="modal" data-target="#<%= div[:id] %>"><%= button[:caption] %></button>
<div class="modal fade" id="<%= div[:id] %>" tabindex="-1" role="dialog" aria-labelledby="label-<%= div[:id] %>" aria-hidden="true">
<div class="modal <%= animation[:class] %>" id="<%= div[:id] %>" tabindex="-1" role="dialog" aria-labelledby="label-<%= div[:id] %>" aria-hidden="true">
<div class="<%= div[:class] %>">
<div class="modal-content">
<div class="modal-header">
Expand All @@ -11,4 +11,4 @@
<%= content %>
</div>
</div>
</div>
</div>
15 changes: 14 additions & 1 deletion spec/shared/modal_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
all_tests_pass_with 'the button: :context modal option'
all_tests_pass_with 'the button: :size modal option'
all_tests_pass_with 'the button: :class modal option'
all_tests_pass_with 'the animated: modal option'
end

#--
Expand Down Expand Up @@ -101,4 +102,16 @@
html = %r{<button class="important btn btn-default"}
expect(modal: {button: {class: 'important'}}).to generate html
end
end
end

shared_examples_for 'the animated: modal option' do
specify 'set to true, keeps the class "fade"' do
html = %r{<div class="modal fade" id=}
expect(modal: {animated: true}).to generate html
end

specify 'set to false, removes the class "fade"' do
html = %r{<div class="modal.*" id=}
expect(modal: {animated: false}).to generate html
end
end