turbo_modal 0.2.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e835c5dd7c4b03134438032640085f017fda7a93143c2db85c0eb5a5a4e5d518
4
- data.tar.gz: a2272f1546ca9584244c062df4e62fbdca5e9e72316fb18aaceefda69500afbc
3
+ metadata.gz: 44e01e6f2393f5d94f8bd717862f49e73e049f550a9916e58ede3b561e5f3f54
4
+ data.tar.gz: 3abc1568da233389ace10ce3ca9c7b092f07833f9b46e45f588de7b4734a850a
5
5
  SHA512:
6
- metadata.gz: 98ef6f9ff6f44877811222effdc0858f076ef3ef42b4f28d88dd2d83639e0fa5bfb370b57b27e9db8080ece7e452e16dcb00b16c41458b96fc33671179f0895a
7
- data.tar.gz: 45d2e51df5a761c24719fb9e1fd068820738d9a1a4f95540c1ef48fa4569bf6aa0b902a30e9f1623d17c1ac70c048fd7492c1f1ee36315fcd1b80cdee5d657e6
6
+ metadata.gz: dde473c24794b73d4991021b5a943619ed21c8ee3a5bdc01f61fe06e3fead2ac5de88c33eb509a3fe505f8aed5bbc1c951938eaa514461f6365f5cb4bc6ddacf
7
+ data.tar.gz: 131c41b10b3eecc05f7a4854d2c478b58d8405f4709d21fd584e9854cf76da690076a5e0ece96ba82466bef2f61bf373f2e0072d5b842ba0615b71c03758cfc4
data/README.md CHANGED
@@ -18,6 +18,15 @@ Turbo modal provides helpers to add a basic scrim and modal target frame. These
18
18
  @import "~@katalyst-interactive/turbo-modal";
19
19
  ```
20
20
 
21
+ ### Import turbo_modal stimulus controllers
22
+ ```js
23
+ /* application.js */
24
+ import TurboModal from "@katalyst-interactive/turbo-modal"
25
+ application.load(TurboModal)
26
+ ```
27
+
28
+ ### Show a modal
29
+
21
30
  To get a modal displaying you will need 2 things:
22
31
  1. A `modal_link` (or programmatic trigger)
23
32
  2. Some `modal_content`
@@ -8,9 +8,8 @@
8
8
  padding: 2rem;
9
9
  --animation-in: slide-in;
10
10
  --animation-out: slide-out;
11
- --mobile-breakpoint: 45rem;
12
11
  }
13
- @media screen and (max-width: var(--mobile-breakpoint)) {
12
+ @media screen and (max-width: 45rem) {
14
13
  .fullscreen .modal {
15
14
  top: 0;
16
15
  left: 0;
@@ -28,7 +27,7 @@
28
27
  --animation-in: slide-up;
29
28
  --animation-out: slide-down;
30
29
  }
31
- @media screen and (max-width: var(--mobile-breakpoint)) {
30
+ @media screen and (max-width: 45rem) {
32
31
  .bottom-sheet .modal {
33
32
  min-height: 65vh;
34
33
  }
@@ -39,7 +38,7 @@
39
38
  animation-duration: 0.25s;
40
39
  animation-fill-mode: forwards;
41
40
  }
42
- @media screen and (max-width: var(--mobile-breakpoint)) {
41
+ @media screen and (max-width: 45rem) {
43
42
  .fullscreen .animate-in {
44
43
  --animation-in: slide-in-left;
45
44
  }
@@ -50,7 +49,7 @@
50
49
  animation-duration: 0.25s;
51
50
  animation-fill-mode: forwards;
52
51
  }
53
- @media screen and (max-width: var(--mobile-breakpoint)) {
52
+ @media screen and (max-width: 45rem) {
54
53
  .fullscreen .animate-out {
55
54
  --animation-out: slide-out-right;
56
55
  }
@@ -1,35 +1,63 @@
1
- .modal {
1
+ .modal-wrapper {
2
2
  position: fixed;
3
- top: 50%;
4
- left: 50%;
3
+ top: 0;
4
+ bottom: 0;
5
+ left: 0;
6
+ right: 0;
5
7
  z-index: 11;
8
+ display: flex;
9
+ justify-content: center;
10
+ align-items: center;
11
+ pointer-events: none;
12
+
13
+ .bottom-sheet & {
14
+ align-items: flex-end;
15
+ }
16
+ }
17
+
18
+ .modal {
19
+ min-width: 40rem;
20
+ max-height: 100%;
21
+ display: flex;
22
+ position: relative;
23
+ top: unset;
24
+ left: unset;
6
25
  background: white;
7
26
  border-radius: 4px;
8
- padding: 2rem;
27
+ padding: 0;
9
28
  --animation-in : slide-in;
10
29
  --animation-out : slide-out;
11
- --mobile-breakpoint: 45rem;
30
+ pointer-events: auto;
31
+ margin: 2rem;
32
+
33
+ @media screen and(max-width: 45rem) {
34
+ min-width: 100%;
35
+ }
36
+
37
+ .modal-content {
38
+ overflow-y: scroll;
39
+ padding: 2rem;
40
+ }
12
41
 
13
42
  .fullscreen & {
14
- @media screen and(max-width: var(--mobile-breakpoint)) {
15
- top: 0;
16
- left: 0;
17
- right: 0;
18
- bottom: 0;
43
+ @media screen and(max-width: 45rem) {
44
+ margin: 0;
19
45
  border-radius: 0;
46
+ height: 100%;
47
+ flex-grow: 1;
20
48
  }
21
49
  }
22
50
 
23
51
  .bottom-sheet & {
24
52
  top: unset;
25
- bottom: 0;
26
- left: 0;
27
- right: 0;
53
+ width: 100%;
54
+ margin: 0;
28
55
  min-height: 40vh;
56
+ transform: unset;
29
57
  --animation-in : slide-up;
30
58
  --animation-out : slide-down;
31
59
 
32
- @media screen and(max-width: var(--mobile-breakpoint)) {
60
+ @media screen and(max-width: 45rem) {
33
61
  min-height: 65vh;
34
62
  }
35
63
  }
@@ -38,10 +66,9 @@
38
66
  .animate-in {
39
67
  animation: var(--animation-in);
40
68
  animation-duration: 0.25s;
41
- animation-fill-mode: forwards;
42
69
 
43
70
  .fullscreen & {
44
- @media screen and(max-width: var(--mobile-breakpoint)) {
71
+ @media screen and(max-width: 45rem) {
45
72
  --animation-in: slide-in-left;
46
73
  }
47
74
  }
@@ -53,7 +80,7 @@
53
80
  animation-fill-mode: forwards;
54
81
 
55
82
  .fullscreen & {
56
- @media screen and(max-width: var(--mobile-breakpoint)) {
83
+ @media screen and(max-width: 45rem) {
57
84
  --animation-out: slide-out-right;
58
85
  }
59
86
  }
@@ -63,24 +90,24 @@
63
90
  @keyframes slide-in {
64
91
  from {
65
92
  opacity: 0;
66
- transform: translate(-50%, -35%);
93
+ transform: translateY(35%);
67
94
  }
68
95
 
69
96
  to {
70
97
  opacity: 1;
71
- transform: translate(-50%, -50%);
98
+ transform: translateY(0%);
72
99
  }
73
100
  }
74
101
 
75
102
  @keyframes slide-out {
76
103
  from {
77
104
  opacity: 1;
78
- transform: translate(-50%, -50%);
105
+ transform: translateY(0%);
79
106
  }
80
107
 
81
108
  to {
82
109
  opacity: 0;
83
- transform: translate(-50%, -35%);
110
+ transform: translateY(35%);
84
111
  }
85
112
  }
86
113
 
@@ -1,8 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ModalHelper
4
- MODAL_ID = "modal"
5
- LINK_OPTIONS = { data: { turbo_frame: MODAL_ID, action: "modal#onLinkClick", modal_type: "" } }.freeze
4
+ MODAL_ID = "modal"
5
+ LINK_OPTIONS = { data: {
6
+ turbo_frame: MODAL_ID,
7
+ controller: "modal-link",
8
+ action: "modal-link#onClick",
9
+ modal_type: "",
10
+ } }.freeze
6
11
 
7
12
  def modal_tag(modal_id = MODAL_ID)
8
13
  turbo_frame_tag modal_id, data: { controller: "modal", modal_target: "turboFrame", action: <<~ACTIONS }
@@ -11,34 +16,30 @@ module ModalHelper
11
16
  ACTIONS
12
17
  end
13
18
 
19
+ # @see ActionView::Helpers::UrlHelper#link_to for argument details
14
20
  def modal_link_to(name = nil, options = nil, html_options = nil, &block)
15
21
  if block
16
- link_to name, LINK_OPTIONS.deep_merge(options || {}), html_options, &block
22
+ link_to name, LINK_OPTIONS.deep_merge(options || {}), &block
17
23
  else
18
- link_to name, options, LINK_OPTIONS.deep_merge(html_options || {}), &block
24
+ link_to name, options, LINK_OPTIONS.deep_merge(html_options || {})
19
25
  end
20
26
  end
21
27
 
22
- def modal_link(text = nil, path = nil, options = {}, &block)
23
- options = DEFAULT_OPTIONS.deep_merge(options)
24
- if block
25
- link_to text || path, options, &block
26
- else
27
- link_to text, path, options
28
- end
29
- end
28
+ alias modal_link modal_link_to
30
29
 
31
30
  def modal_content(options = {}, &block)
32
31
  modal_id = options.fetch(:modal_id, MODAL_ID)
33
32
  turbo_frame_tag modal_id, data: { hidden: "" } do
34
- tag.div(class: "modal animate-in #{options.delete(:modal_classes)}",
35
- data: {
36
- action: <<~ACTIONS
37
- keyup@window->modal#keyup
38
- scrim:hide@window->modal#close
39
- ACTIONS
40
- }) do
41
- yield if block
33
+ tag.div(class: "modal-wrapper") do
34
+ tag.div(class: "modal animate-in #{options.delete(:modal_classes)}",
35
+ data: {
36
+ action: <<~ACTIONS
37
+ keyup@window->modal#keyup
38
+ scrim:hide@window->modal#close
39
+ ACTIONS
40
+ }) do
41
+ yield if block
42
+ end
42
43
  end
43
44
  end
44
45
  end
@@ -1,4 +1,16 @@
1
1
  import ModalController, { openModal, closeModal } from "./modal_controller";
2
+ import ModalLinkController from "./modal_link_controller";
2
3
  import ScrimController, { showScrim, hideScrim } from "./scrim_controller";
3
4
 
4
- export { ModalController, openModal, closeModal, ScrimController, showScrim, hideScrim };
5
+ const Definitions = [
6
+ { identifier: "modal", controllerConstructor: ModalController },
7
+ { identifier: "modal-link", controllerConstructor: ModalLinkController },
8
+ { identifier: "scrim", controllerConstructor: ScrimController }
9
+ ];
10
+
11
+ export {
12
+ Definitions as default,
13
+ ModalController, openModal, closeModal,
14
+ ScrimController, showScrim, hideScrim,
15
+ ModalLinkController
16
+ };
@@ -2,7 +2,7 @@ import { Controller } from "stimulus";
2
2
  import { hideScrim, showScrim } from "./scrim_controller";
3
3
 
4
4
  /**
5
- * Shows a Turbo modal when triggered by a `modal_link` click or a call to `openModal`.
5
+ * Shows a Turbo modal when triggered by a `modal:open` event (e.g. `openModal`).
6
6
  */
7
7
  class ModalController extends Controller {
8
8
  static targets = ["turboFrame"];
@@ -45,11 +45,6 @@ class ModalController extends Controller {
45
45
  closeModal();
46
46
  }
47
47
  }
48
-
49
- onLinkClick(e) {
50
- const modal_link = e.target.closest("a[data-modal-type]")
51
- openModal(modal_link.href, modal_link.dataset.modalType);
52
- }
53
48
  }
54
49
 
55
50
  /**
@@ -0,0 +1,12 @@
1
+ import { Controller } from "stimulus";
2
+ import { openModal } from "./modal_controller";
3
+
4
+ /**
5
+ * Shows a Turbo modal when clicked.
6
+ */
7
+ export default class ModalLinkController extends Controller {
8
+ onClick(e) {
9
+ e.preventDefault();
10
+ openModal(this.element.href, this.element.dataset.modalType);
11
+ }
12
+ }
@@ -15,15 +15,25 @@ class ScrimController extends Controller {
15
15
 
16
16
  show(event) {
17
17
  delete this.scrimTarget.dataset.hidden;
18
+ document.body.style.height = "100vh";
19
+ document.body.style.overflow = "hidden";
18
20
  }
19
21
 
20
22
  hide(event) {
21
23
  this.scrimTarget.dataset.hidden = "";
24
+ unclipScreen();
22
25
  }
23
26
 
24
27
  onClick(event) {
25
28
  window.dispatchEvent(new Event("scrim:hide"));
26
29
  }
30
+
31
+ disconnect() {
32
+ this.scrimTarget.dataset.hidden = "";
33
+ unclipScreen();
34
+ super.disconnect();
35
+ }
36
+
27
37
  }
28
38
 
29
39
  /**
@@ -40,4 +50,12 @@ function hideScrim() {
40
50
  window.dispatchEvent(new Event("scrim:hide"));
41
51
  }
42
52
 
53
+ /**
54
+ * Unclips body to viewport size
55
+ */
56
+ function unclipScreen() {
57
+ document.body.style.height = "unset";
58
+ document.body.style.overflow = "unset";
59
+ }
60
+
43
61
  export { ScrimController as default, showScrim, hideScrim }
@@ -20,6 +20,5 @@ say "Install Turbo Modal"
20
20
  say "Turbo Modal #{TurboModal::VERSION} requires Stimulus@2.0.0", :yellow
21
21
  run "yarn add @katalyst-interactive/turbo-modal"
22
22
 
23
- append_to_file "app/javascript/controllers/index.js", %(\nimport { ScrimController, ModalController } from "@katalyst-interactive/turbo-modal"\n)
24
- append_to_file "app/javascript/controllers/index.js", %(application.register("scrim", ScrimController)\n)
25
- append_to_file "app/javascript/controllers/index.js", %(application.register("modal", ModalController)\n)
23
+ append_to_file "app/javascript/controllers/index.js", %(\nimport TurboModal from "@katalyst-interactive/turbo-modal"\n)
24
+ append_to_file "app/javascript/controllers/index.js", %(application.load(TurboModal)\n)
@@ -1,3 +1,3 @@
1
1
  module TurboModal
2
- VERSION = '0.2.0'
2
+ VERSION = "0.3.1".freeze
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: turbo_modal
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alan Cornthwaite
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2022-03-10 00:00:00.000000000 Z
12
+ date: 2022-05-09 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -60,6 +60,7 @@ files:
60
60
  - app/helpers/scrim_helper.rb
61
61
  - app/javascript/turbo_modal/index.js
62
62
  - app/javascript/turbo_modal/modal_controller.js
63
+ - app/javascript/turbo_modal/modal_link_controller.js
63
64
  - app/javascript/turbo_modal/scrim_controller.js
64
65
  - config/routes.rb
65
66
  - lib/install/turbo_modal.rb
@@ -90,7 +91,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
90
91
  - !ruby/object:Gem::Version
91
92
  version: '0'
92
93
  requirements: []
93
- rubygems_version: 3.2.32
94
+ rubygems_version: 3.1.6
94
95
  signing_key:
95
96
  specification_version: 4
96
97
  summary: Modal library that uses Turbo and Stimulus because we are in the future now.