playbook_ui 14.18.0.pre.rc.4 → 14.18.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 (49) hide show
  1. checksums.yaml +4 -4
  2. data/app/pb_kits/playbook/pb_circle_icon_button/_circle_icon_button.scss +39 -1
  3. data/app/pb_kits/playbook/pb_circle_icon_button/_circle_icon_button.tsx +4 -0
  4. data/app/pb_kits/playbook/pb_circle_icon_button/circle_icon_button.rb +9 -1
  5. data/app/pb_kits/playbook/pb_circle_icon_button/circle_icon_button.test.js +14 -0
  6. data/app/pb_kits/playbook/pb_circle_icon_button/docs/_circle_icon_button_size.html.erb +28 -0
  7. data/app/pb_kits/playbook/pb_circle_icon_button/docs/_circle_icon_button_size.jsx +42 -0
  8. data/app/pb_kits/playbook/pb_circle_icon_button/docs/_circle_icon_button_size.md +1 -0
  9. data/app/pb_kits/playbook/pb_circle_icon_button/docs/example.yml +2 -0
  10. data/app/pb_kits/playbook/pb_circle_icon_button/docs/index.js +1 -0
  11. data/app/pb_kits/playbook/pb_copy_button/docs/_copy_button_hook_rails.html.erb +14 -0
  12. data/app/pb_kits/playbook/pb_copy_button/docs/_copy_button_hook_rails.md +3 -0
  13. data/app/pb_kits/playbook/pb_copy_button/docs/example.yml +1 -0
  14. data/app/pb_kits/playbook/pb_copy_button/index.js +46 -20
  15. data/app/pb_kits/playbook/pb_dialog/dialog.html.erb +10 -1
  16. data/app/pb_kits/playbook/pb_dialog/dialog.rb +2 -0
  17. data/app/pb_kits/playbook/pb_dialog/docs/_dialog_turbo_frames.html.erb +117 -0
  18. data/app/pb_kits/playbook/pb_dialog/docs/_dialog_turbo_frames_rails.md +9 -0
  19. data/app/pb_kits/playbook/pb_dialog/docs/example.yml +1 -0
  20. data/app/pb_kits/playbook/pb_dialog/index.js +106 -14
  21. data/app/pb_kits/playbook/pb_gauge/docs/_gauge_pb_styles.jsx +31 -0
  22. data/app/pb_kits/playbook/pb_gauge/docs/_gauge_pb_styles.md +1 -0
  23. data/app/pb_kits/playbook/pb_gauge/docs/example.yml +1 -0
  24. data/app/pb_kits/playbook/pb_gauge/docs/index.js +1 -0
  25. data/app/pb_kits/playbook/pb_gauge/gaugeTheme.ts +83 -0
  26. data/app/pb_kits/playbook/pb_overlay/_overlay.scss +34 -0
  27. data/app/pb_kits/playbook/pb_overlay/_overlay.tsx +25 -4
  28. data/app/pb_kits/playbook/pb_overlay/docs/_overlay_fullscreen_background.jsx +49 -0
  29. data/app/pb_kits/playbook/pb_overlay/docs/_overlay_fullscreen_background.md +1 -0
  30. data/app/pb_kits/playbook/pb_overlay/docs/_overlay_fullscreen_opacity.jsx +81 -0
  31. data/app/pb_kits/playbook/pb_overlay/docs/_overlay_fullscreen_opacity.md +1 -0
  32. data/app/pb_kits/playbook/pb_overlay/docs/example.yml +2 -0
  33. data/app/pb_kits/playbook/pb_overlay/docs/index.js +2 -0
  34. data/dist/chunks/_typeahead-C_FX7o_S.js +22 -0
  35. data/dist/chunks/{_weekday_stacked-Bm5175-u.js → _weekday_stacked-B4RLTeDo.js} +2 -2
  36. data/dist/chunks/lazysizes-DHz07jlL.js +1 -0
  37. data/dist/chunks/lib-ySl8uEpT.js +29 -0
  38. data/dist/chunks/{pb_form_validation-Bx1OK6xs.js → pb_form_validation-BRjyJrKh.js} +1 -1
  39. data/dist/chunks/vendor.js +1 -1
  40. data/dist/menu.yml +2 -2
  41. data/dist/playbook-doc.js +1 -1
  42. data/dist/playbook-rails-react-bindings.js +1 -1
  43. data/dist/playbook-rails.js +1 -1
  44. data/dist/playbook.css +1 -1
  45. data/lib/playbook/version.rb +1 -1
  46. metadata +21 -7
  47. data/dist/chunks/_typeahead-aRwivA3u.js +0 -22
  48. data/dist/chunks/lazysizes-B7xYodB-.js +0 -1
  49. data/dist/chunks/lib-Cj4H6j1c.js +0 -29
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bbb4292478c72d434ee753aec73a3f805c891f91ec56e473fc218235f7db6838
4
- data.tar.gz: 9e07e43e6d9da0149a3472a74539b092d6743a425905377ab55938667e745984
3
+ metadata.gz: 94ec449a03aebe3006ca1edc972e4632eb4ce67276e4e2a575dd54e4a26d87be
4
+ data.tar.gz: 6843d0fe6728e70935869341db3aa00e22eaa987e935ff80c502629f841fee9b
5
5
  SHA512:
6
- metadata.gz: a3f6051e964012be59826c377cd47aac04f6e14330026b2f7daee6dfa4c0798ebc6fb2a0d866411fdbc2a6ed02876fb6f505b4026664b0719b4d725570db72ec
7
- data.tar.gz: 5e1bbb99105ed4d85d434f7381d2e6f8670bd46d5a5209219d55ce163524b8bbdf03d5fc9e4e6f27723c482921907e817cf38b4a410ab050dac749c6621113a0
6
+ metadata.gz: c4f7a5ceff1a63c95bfea4df95fcc16a633fd926d6a521cf812062292da2cc73ddaf8b3e010311bcedd20b962a7558855c44895d236ab5db3b0015cda458fb1c
7
+ data.tar.gz: ed2800f912e35e9e0c2e457613ad19d58e9801b9fc8ce8a322c38d0d0609b28138eaeb9dcce449e441ceb93983697c628b49790eccc930c9264e4e9c11fbc505
@@ -53,4 +53,42 @@ $pb_button_styles: (
53
53
  }
54
54
  }
55
55
  }
56
- }
56
+ &.size_small {
57
+ @each $style in $pb_button_styles {
58
+ [class^=pb_button_kit][class*=_#{$style}] {
59
+ width: 20px;
60
+ height: 20px;
61
+ min-height: 20px;
62
+ border-radius: 10px;
63
+ line-height: 20px;
64
+ flex-basis: 20px;
65
+ min-width: 20px;
66
+ padding: 0;
67
+ }
68
+ }
69
+ // centering small icon svg
70
+ .pb_button_content {
71
+ display: flex;
72
+ align-items: center;
73
+ justify-content: center;
74
+ // rails
75
+ svg.pb_custom_icon {
76
+ height: 14px;
77
+ width: 14px;
78
+ vertical-align: middle;
79
+ }
80
+ // react (nested within an additional span)
81
+ > span {
82
+ display: flex;
83
+ align-items: center;
84
+ justify-content: center;
85
+ > svg.pb_custom_icon {
86
+ height: 14px;
87
+ width: 14px;
88
+ vertical-align: middle;
89
+ display: inline-block;
90
+ }
91
+ }
92
+ }
93
+ }
94
+ }
@@ -22,6 +22,7 @@ type CircleIconButtonProps = {
22
22
  type?: 'button' | 'submit' | 'reset' | undefined,
23
23
  target?: string
24
24
  variant?: 'primary' | 'secondary' | 'link',
25
+ size?: 'default' | 'sm',
25
26
  }
26
27
 
27
28
  const CircleIconButton = (props: CircleIconButtonProps): React.ReactElement => {
@@ -41,13 +42,16 @@ const CircleIconButton = (props: CircleIconButtonProps): React.ReactElement => {
41
42
  link,
42
43
  newWindow,
43
44
  variant,
45
+ size = 'default',
44
46
  } = props
45
47
 
46
48
  const ariaProps = buildAriaProps(aria)
47
49
  const dataProps = buildDataProps(data)
48
50
  const htmlProps = buildHtmlProps(htmlOptions)
51
+ const sizeClass = size === 'sm' ? 'size_small' : ''
49
52
  const classes = classnames(
50
53
  buildCss('pb_circle_icon_button_kit'),
54
+ sizeClass,
51
55
  globalProps(props),
52
56
  className
53
57
  )
@@ -18,8 +18,16 @@ module Playbook
18
18
  prop :new_window, type: Playbook::Props::Boolean,
19
19
  default: false
20
20
  prop :target
21
+ prop :size, type: Playbook::Props::Enum,
22
+ values: %w[default sm],
23
+ default: "default"
24
+
21
25
  def classname
22
- generate_classname("pb_circle_icon_button_kit")
26
+ generate_classname("pb_circle_icon_button_kit") + size_class
27
+ end
28
+
29
+ def size_class
30
+ size == "sm" ? " size_small" : ""
23
31
  end
24
32
  end
25
33
  end
@@ -30,3 +30,17 @@ test('passes loading prop to button', () => {
30
30
 
31
31
  expect(button).toBeInTheDocument()
32
32
  })
33
+
34
+ test('adds size_small class when size is sm', () => {
35
+ render(
36
+ <CircleIconButton
37
+ data={{ testid: 'small-size-test' }}
38
+ icon="plus"
39
+ size="sm"
40
+ />
41
+ )
42
+
43
+ const kit = screen.getByTestId('small-size-test')
44
+
45
+ expect(kit).toHaveClass('pb_circle_icon_button_kit size_small')
46
+ })
@@ -0,0 +1,28 @@
1
+ <%= pb_rails("flex", props: { orientation: "column", gap: "sm" }) do %>
2
+ <%= pb_rails("circle_icon_button", props: {
3
+ icon: "plus",
4
+ size: "sm",
5
+ variant: "primary"
6
+ }) %>
7
+
8
+ <%= pb_rails("circle_icon_button", props: {
9
+ icon: "pen",
10
+ loading: true,
11
+ size: "sm",
12
+ variant: "secondary"
13
+ }) %>
14
+
15
+ <%= pb_rails("circle_icon_button", props: {
16
+ disabled: true,
17
+ icon: "times",
18
+ size: "sm"
19
+ }) %>
20
+
21
+ <%= pb_rails("circle_icon_button", props: {
22
+ icon: "info",
23
+ link: "https://playbook.powerapp.cloud/",
24
+ size: "sm",
25
+ target: "child",
26
+ variant: "link"
27
+ }) %>
28
+ <% end %>
@@ -0,0 +1,42 @@
1
+ import React from 'react'
2
+
3
+ import CircleIconButton from '../_circle_icon_button'
4
+ import Flex from '../../pb_flex/_flex'
5
+
6
+ const CircleIconButtonSize = (props) => (
7
+ <>
8
+ <Flex gap="sm"
9
+ orientation="column"
10
+ >
11
+ <CircleIconButton
12
+ icon="plus"
13
+ size="sm"
14
+ variant="primary"
15
+ {...props}
16
+ />
17
+ <CircleIconButton
18
+ icon="pen"
19
+ loading
20
+ size="sm"
21
+ variant="secondary"
22
+ {...props}
23
+ />
24
+ <CircleIconButton
25
+ disabled
26
+ icon="times"
27
+ size="sm"
28
+ {...props}
29
+ />
30
+ <CircleIconButton
31
+ icon="info"
32
+ link="https://playbook.powerapp.cloud/"
33
+ size="sm"
34
+ target="child"
35
+ variant="link"
36
+ {...props}
37
+ />
38
+ </Flex>
39
+ </>
40
+ )
41
+
42
+ export default CircleIconButtonSize
@@ -0,0 +1 @@
1
+ The `size` prop accepts "sm" as a value to make the Circle Icon Button 20px x 20px instead of the default 40px x 40px size.
@@ -4,9 +4,11 @@ examples:
4
4
  - circle_icon_button_default: Default
5
5
  - circle_icon_button_link: Link
6
6
  - circle_icon_button_loading: Loading
7
+ - circle_icon_button_size: Size
7
8
 
8
9
  react:
9
10
  - circle_icon_button_default: Default
10
11
  - circle_icon_button_click: Click Handler
11
12
  - circle_icon_button_link: Link
12
13
  - circle_icon_button_loading: Loading
14
+ - circle_icon_button_size: Size
@@ -2,3 +2,4 @@ export { default as CircleIconButtonDefault } from './_circle_icon_button_defaul
2
2
  export { default as CircleIconButtonClick } from './_circle_icon_button_click.jsx'
3
3
  export { default as CircleIconButtonLink } from './_circle_icon_button_link.jsx'
4
4
  export { default as CircleIconButtonLoading } from './_circle_icon_button_loading.jsx'
5
+ export { default as CircleIconButtonSize } from './_circle_icon_button_size.jsx'
@@ -0,0 +1,14 @@
1
+ <%= pb_rails("body", props: { cursor: "pointer", data: { "external-copy-from": "hookbody"} }) do %>
2
+ <span id="hookbody">I'm a custom copy hook! Click this body to copy this text!</span>
3
+ <% end %>
4
+
5
+ <%= pb_rails("tooltip", props: {
6
+ trigger_element_selector: "#hookbody",
7
+ tooltip_id: "hookbody_tooltip",
8
+ position: 'top',
9
+ trigger_method: "click"
10
+ }) do %>
11
+ Copied!
12
+ <% end %>
13
+
14
+ <%= pb_rails("textarea", props: { margin_top: "xs", placeholder: "Paste here" }) %>
@@ -0,0 +1,3 @@
1
+ You can use any external control (like the text itself or an icon) to copy. To use this hook, set the `data-external-copy-from` attribute on the external control to the copying ID. This will grab the `innerText` from `your_id` element, or `value` if it is an input element. Alternatively, you can set a `data-external-copy-value` attribute and copy custom text.
2
+
3
+ See the code example for details.
@@ -2,6 +2,7 @@ examples:
2
2
  rails:
3
3
  - copy_button_default: Default
4
4
  - copy_button_from: Copy From
5
+ - copy_button_hook_rails: Copy Hook
5
6
 
6
7
  react:
7
8
  - copy_button_default: Default
@@ -1,6 +1,33 @@
1
1
  import PbEnhancedElement from "../pb_enhanced_element"
2
2
 
3
- export default class PbCopyButton extends PbEnhancedElement {
3
+ function getTextFromElement(element) {
4
+ if (!element) return ''
5
+ return element.tagName.toLowerCase() === 'input'
6
+ ? element.value
7
+ : element.innerText
8
+ }
9
+
10
+ function copyTextToClipboard(text) {
11
+ if (!text) return
12
+ navigator.clipboard.writeText(text)
13
+ .catch(err => console.error('Failed to copy text:', err))
14
+ }
15
+
16
+ function handleExternalControlCopyClick(element) {
17
+ const value = element.getAttribute('data-external-copy-value')
18
+ const fromId = element.getAttribute('data-external-copy-from')
19
+
20
+ if (value) {
21
+ copyTextToClipboard(value)
22
+ } else if (fromId) {
23
+ const fromElement = document.querySelector(`#${fromId}`)
24
+ copyTextToClipboard(getTextFromElement(fromElement))
25
+ } else {
26
+ console.warn('Failed to copy:', element)
27
+ }
28
+ }
29
+
30
+ export class PbCopyButton extends PbEnhancedElement {
4
31
  static get selector() {
5
32
  return '.pb_copy_button_kit'
6
33
  }
@@ -20,28 +47,27 @@ export default class PbCopyButton extends PbEnhancedElement {
20
47
  }
21
48
 
22
49
  handleClick() {
50
+ const value = this.element.getAttribute('data-copy-value')
23
51
  const fromId = this.element.getAttribute('data-from')
24
- if (fromId) {
25
- const fromElement = document.querySelector(`#${fromId}`)
26
- if (fromElement) {
27
- let contentToCopy = ''
28
- if (fromElement.tagName.toLowerCase() === 'input') {
29
- contentToCopy = fromElement.value
30
- } else {
31
- contentToCopy = fromElement.innerText
32
- }
33
- navigator.clipboard.writeText(contentToCopy)
34
- .catch(err => console.error('Failed to copy text', err))
35
- return
36
- }
37
- }
38
52
 
39
- const textToCopy = this.element.getAttribute('data-copy-value')
40
- if (textToCopy) {
41
- navigator.clipboard.writeText(textToCopy)
42
- .catch(err => console.error('Failed to copy text', err))
53
+ if (value) {
54
+ copyTextToClipboard(value)
55
+ } else if (fromId) {
56
+ const fromElement = document.querySelector(`#${fromId}`)
57
+ copyTextToClipboard(getTextFromElement(fromElement))
43
58
  } else {
44
- console.warn('No data-copy-value attribute found or data-from element')
59
+ console.warn('No data-copy-value or data-from attribute found')
45
60
  }
46
61
  }
47
62
  }
63
+
64
+ export function addCopyEventListeners() {
65
+ const externalCopyElements = [
66
+ ...document.querySelectorAll('div[data-external-copy-value]'),
67
+ ...document.querySelectorAll('div[data-external-copy-from]')
68
+ ]
69
+
70
+ externalCopyElements.forEach(element => {
71
+ element.addEventListener('click', () => handleExternalControlCopyClick(element))
72
+ })
73
+ }
@@ -1,7 +1,16 @@
1
1
  <div
2
2
  class="pb_dialog_wrapper_rails <%= object.full_height_style %>"
3
3
  data-pb-dialog-wrapper="true"
4
- data-overlay-click= <%= object.overlay_close %>
4
+ data-overlay-click="<%= object.overlay_close %>"
5
+ <% if object.custom_event_type.present? %>
6
+ data-custom-event-type="<%= object.custom_event_type %>"
7
+ <% end %>
8
+ <% if object.confirm_button_id.present? %>
9
+ data-confirm-button-id="<%= object.confirm_button_id %>"
10
+ <% end %>
11
+ <% if object.cancel_button_id.present? %>
12
+ data-cancel-button-id="<%= object.cancel_button_id %>"
13
+ <% end %>
5
14
  >
6
15
  <%= pb_content_tag(:dialog) do %>
7
16
  <% if object.status === "" && object.title %>
@@ -21,6 +21,8 @@ module Playbook
21
21
  prop :status, type: Playbook::Props::Enum,
22
22
  values: ["info", "caution", "delete", "error", "success", "default", ""],
23
23
  default: ""
24
+ prop :custom_event_type, type: Playbook::Props::String,
25
+ default: ""
24
26
 
25
27
  def classname
26
28
  generate_classname("pb_dialog pb_dialog_rails pb_dialog_#{size}_#{placement}")
@@ -0,0 +1,117 @@
1
+ <%= pb_rails("flex", props:{ gap: "xs", wrap:true}) do %>
2
+ <%= pb_rails("button", props: { id: "button-open-example-turbo-frames", margin_right: "md", text: "Open Dialog via Custom Event" }) %>
3
+ <%= pb_rails("button", props: { id: "open-dialog-button", data: { open_dialog: "dialog-confirm-turbo-frames" }, margin_right: "md", text: "Custom Event Linked to Confirm Button" }) %>
4
+ <%= pb_rails("button", props: { id: "button-open-multi-dialog", text: "Mutliple Events Example" }) %>
5
+ <% end %>
6
+
7
+ <!-- Example 1: Basic dialog with custom event opening -->
8
+ <%= pb_rails("dialog", props: {
9
+ id: "dialog-open-turbo-frames",
10
+ title: "Click Event Simulation",
11
+ text: "Demonstrating Opening the Dialog with a Custom Event.",
12
+ custom_event_type: "dialogOpen",
13
+ cancel_button: "Cancel Button",
14
+ confirm_button: "Okay",
15
+ confirm_button_id: "confirm-button-turbo-frames"
16
+ }) %>
17
+
18
+ <script>
19
+ document.getElementById("button-open-example-turbo-frames").addEventListener("click", () => {
20
+ console.log("📣 Dispatching 'dialogOpen' custom event")
21
+
22
+ window.dispatchEvent(new CustomEvent("dialogOpen", {
23
+ bubbles: true,
24
+ detail: {
25
+ dialogId: "dialog-open-turbo-frames",
26
+ action: "open"
27
+ }
28
+ }))
29
+
30
+ console.log("✅ Custom event dispatched - dialog should open")
31
+ })
32
+ </script>
33
+
34
+ <!-- Example 2: Dialog with custom event linking confirm button to closing the dialog-->
35
+ <%= pb_rails("dialog", props: {
36
+ id: "dialog-confirm-turbo-frames",
37
+ title: "Custom Event Button Action",
38
+ text: "Clicking this dialog's confirm button triggers closing the dialog as well.",
39
+ custom_event_type: "turboResponse",
40
+ cancel_button: "Cancel",
41
+ cancel_button_id: "cancel-button-id-turbo-frames",
42
+ confirm_button: "Confirm Button",
43
+ confirm_button_id: "confirm-button-id-turbo-frames"
44
+ }) %>
45
+
46
+ <script>
47
+ window.addEventListener("turboResponse", (event) => {
48
+ const { dialogId, action, cancelButtonId } = event.detail || {}
49
+ console.log("📦 turboResponse event triggered:", { dialogId, action, cancelButtonId })
50
+
51
+ if (action === "close" && cancelButtonId) {
52
+ const dialog = document.getElementById(dialogId)
53
+ if (dialog?.close) {
54
+ console.log("🚪 Closing dialog programmatically")
55
+ dialog.close()
56
+ }
57
+ }
58
+ })
59
+
60
+ document.getElementById("confirm-button-id-turbo-frames").addEventListener("click", function () {
61
+ console.log("✅ Confirm button clicked! Triggering cancel via turboResponse")
62
+
63
+ window.dispatchEvent(new CustomEvent("turboResponse", {
64
+ detail: {
65
+ dialogId: "dialog-confirm-turbo-frames",
66
+ action: "close",
67
+ cancelButtonId: "cancel-button-id-turbo-frames"
68
+ }
69
+ }))
70
+ })
71
+ </script>
72
+
73
+ <!-- Example 3: Dialog with multiple custom event types -->
74
+ <%= pb_rails("dialog", props: {
75
+ id: "multi-event-dialog",
76
+ title: "Multiple Event Types",
77
+ text: "This dialog responds to multiple custom event types - see console logs.",
78
+ custom_event_type: "dialogOpenMutli,turboResponseMulti",
79
+ cancel_button: "Cancel",
80
+ cancel_button_id: "multi-event-cancel",
81
+ confirm_button: "Confirm",
82
+ confirm_button_id: "multi-event-confirm"
83
+ }) %>
84
+
85
+ <script>
86
+ document.getElementById("button-open-multi-dialog").addEventListener("click", () => {
87
+ console.log("📣 Dispatching 'dialogOpenMutli' custom event")
88
+ window.dispatchEvent(new CustomEvent("dialogOpenMutli", {
89
+ detail: {
90
+ dialogId: "multi-event-dialog",
91
+ action: "open"
92
+ }
93
+ }))
94
+ })
95
+
96
+ document.getElementById("multi-event-confirm")?.addEventListener("click", function() {
97
+ console.log("✅ Confirm clicked — dispatching 'turboResponseMulti' to simulate dialog close")
98
+
99
+ window.dispatchEvent(new CustomEvent("turboResponseMulti", {
100
+ detail: {
101
+ dialogId: "multi-event-dialog",
102
+ action: "close",
103
+ cancelButtonId: "multi-event-cancel"
104
+ }
105
+ }))
106
+ })
107
+
108
+ window.addEventListener("turboResponseMulti", (event) => {
109
+ const { dialogId, action, cancelButtonId } = event.detail || {}
110
+
111
+ if (action === "close" && dialogId) {
112
+ const dialog = document.getElementById(dialogId)
113
+ dialog.close?.()
114
+ console.log("🚪 Closing dialog programmatically")
115
+ }
116
+ })
117
+ </script>
@@ -0,0 +1,9 @@
1
+ The `custom_event_type` prop allows you to specify custom events that will trigger the dialog's initialization or control its behavior. This is especially useful when working with Turbo Frame updates where standard DOM events might not suffice.
2
+
3
+ The examples demonstrate two use cases:
4
+ 1) Opening a dialog via custom event dispatch: The first example shows how to configure a dialog to listen for a specific custom event (dialogOpen). When this event is dispatched, the dialog will automatically open, making it easy to trigger the dialog from JavaScript or after Turbo Frame operations.
5
+ 2) Controlling dialog button actions with custom events: The second example demonstrates how to set up a dialog that can have its confirm button triggered through external events (turboResponse). This pattern is useful when you need to programmatically confirm a dialog after some background operation completes.
6
+ 3) Multiple custom events: The third example combines the first two to show how `custom_event_type` prop can support multiple event types separated by a comma.
7
+
8
+ For Turbo integration, you can use standard Turbo events like turbo:frame-load or turbo:submit-end as your `custom_event_type` to ensure the dialog responds properly after Turbo navigation or form submissions. The dialog component will listen for these events automatically.
9
+ The implementation handles various actions including 'open', 'close', 'clickConfirm', and 'clickCancel', making it flexible for different interaction patterns in your Turbo-enhanced application.
@@ -11,6 +11,7 @@ examples:
11
11
  - dialog_full_height: Full Height
12
12
  - dialog_full_height_placement: Full Height Placement
13
13
  - dialog_loading: Loading
14
+ - dialog_turbo_frames: Within Turbo Frames
14
15
 
15
16
 
16
17
  react:
@@ -10,6 +10,81 @@ export default class PbDialog extends PbEnhancedElement {
10
10
  connect() {
11
11
  window.addEventListener("DOMContentLoaded", () => this.setupDialog())
12
12
  window.addEventListener("turbo:frame-load", () => this.setupDialog())
13
+
14
+ // Code for custom_event_type setup (can take multiple events in a string separated by commas)
15
+ const customEventTypeString = this.element.dataset.customEventType
16
+ if (customEventTypeString && !this.element.hasAttribute("data-custom-event-handled")) {
17
+ this.customEventTypes = customEventTypeString.split(",").map(e => e.trim()).filter(Boolean)
18
+ this.customEventTypes.forEach(eventType => {
19
+ window.addEventListener(eventType, this.handleCustomEvent)
20
+ })
21
+
22
+ this.element.setAttribute("data-custom-event-handled", "true")
23
+ }
24
+ }
25
+
26
+ disconnect() {
27
+ if (this.customEventTypes && Array.isArray(this.customEventTypes)) {
28
+ this.customEventTypes.forEach(eventType => {
29
+ window.removeEventListener(eventType, this.handleCustomEvent)
30
+ })
31
+ }
32
+ }
33
+
34
+ handleCustomEvent = (event) => {
35
+ const dialogId = event.detail?.dialogId || this.element.querySelector("dialog")?.id
36
+ const dialog = dialogId && document.getElementById(dialogId)
37
+
38
+ if (!dialog) {
39
+ console.warn(`[PbDialog] Could not find dialog with ID '${dialogId}'`)
40
+ return
41
+ }
42
+
43
+ this.setupDialog()
44
+
45
+ const action = event.detail?.action || 'open'
46
+
47
+ // Known Actions - four "standard" dialog actions that felt like things devs might want to do
48
+ const knownActions = ['open', 'close', 'clickConfirm', 'clickCancel'];
49
+
50
+ if (knownActions.includes(action)) {
51
+ switch (action) {
52
+ case 'open':
53
+ if (!dialog.open) dialog.showModal()
54
+ break
55
+ case 'close':
56
+ if (dialog.open) dialog.close(event.detail?.returnValue)
57
+ break
58
+ case 'clickConfirm':
59
+ this.triggerButtonClick(dialog, event, 'confirm')
60
+ break
61
+ case 'clickCancel':
62
+ this.triggerButtonClick(dialog, event, 'cancel')
63
+ break
64
+ }
65
+ }
66
+ // Custom Actions - flexible for Turbo or other integration
67
+ else if (typeof event.detail?.customAction === 'function') {
68
+ event.detail.customAction({ dialog, event })
69
+ } else if (window.pbDialogActions?.[action]) {
70
+ // Can register custom actions globally
71
+ window.pbDialogActions[action]({ dialog, event })
72
+ } else {
73
+ console.warn(`[PbDialog] Unknown action: ${action}`)
74
+ }
75
+ }
76
+
77
+ triggerButtonClick(dialog, event, type) {
78
+ const buttonId = event.detail?.[`${type}ButtonId`] ||
79
+ dialog.closest('[data-pb-dialog-wrapper]')?.dataset[`${type}ButtonId`]
80
+ const button = buttonId ? document.getElementById(buttonId) :
81
+ dialog.querySelector(`[data-${type}-button]`)
82
+
83
+ if (button) {
84
+ button.click()
85
+ } else {
86
+ console.warn(`[PbDialog] Could not find ${type} button for dialog`)
87
+ }
13
88
  }
14
89
 
15
90
  setupDialog() {
@@ -18,7 +93,7 @@ export default class PbDialog extends PbEnhancedElement {
18
93
  const dialogs = document.querySelectorAll(".pb_dialog_rails")
19
94
 
20
95
  const loadingButton = document.querySelector('[data-disable-with="Loading"]');
21
- if (loadingButton) {
96
+ if (loadingButton && !loadingButton.dataset.listenerAttached) {
22
97
  loadingButton.addEventListener("click", function () {
23
98
  const okayLoadingButton = document.querySelector('[data-disable-with="Loading"]');
24
99
  const cancelButton = document.querySelector('[data-disable-cancel-with="Loading"]');
@@ -35,27 +110,42 @@ export default class PbDialog extends PbEnhancedElement {
35
110
  okayLoadingButton.className = newClass;
36
111
  if (cancelButton) cancelButton.className = newCancelClass;
37
112
  });
113
+
114
+ // Prevent multiple registrations
115
+ loadingButton.dataset.listenerAttached = "true";
38
116
  }
39
-
117
+
40
118
  openTrigger.forEach((open) => {
41
- open.addEventListener("click", () => {
42
- var openTriggerData = open.dataset.openDialog;
43
- var targetDialog = document.getElementById(openTriggerData)
44
- if (targetDialog.open) return;
45
- targetDialog.showModal();
46
- });
119
+ const originalClickHandler = open._openDialogClickHandler
120
+ if (originalClickHandler) open.removeEventListener("click", originalClickHandler)
121
+
122
+ open._openDialogClickHandler = () => {
123
+ const openTriggerData = open.dataset.openDialog;
124
+ const targetDialogOpen = document.getElementById(openTriggerData)
125
+ if (targetDialogOpen && !targetDialogOpen.open) targetDialogOpen.showModal()
126
+ };
127
+
128
+ open.addEventListener("click", open._openDialogClickHandler)
47
129
  });
48
130
 
49
131
  closeTrigger.forEach((close) => {
50
- close.addEventListener("click", () => {
51
- var closeTriggerData = close.dataset.closeDialog;
52
- document.getElementById(closeTriggerData).close();
53
- });
132
+ const originalClickHandler = close._closeDialogClickHandler
133
+ if (originalClickHandler) close.removeEventListener("click", originalClickHandler)
134
+
135
+ close._closeDialogClickHandler = () => {
136
+ const closeTriggerData = close.dataset.closeDialog;
137
+ const targetDialogClose = document.getElementById(closeTriggerData)
138
+ if (targetDialogClose) targetDialogClose.close();
139
+ };
140
+
141
+ close.addEventListener("click", close._closeDialogClickHandler)
54
142
  });
55
143
 
56
144
  // Close dialog box on outside click
57
145
  dialogs.forEach((dialogElement) => {
58
- dialogElement.addEventListener("mousedown", (event) => {
146
+ const originalMousedownHandler = dialogElement._outsideClickHandler
147
+ if (originalMousedownHandler) dialogElement.removeEventListener("mousedown", originalMousedownHandler)
148
+ dialogElement._outsideClickHandler = (event) => {
59
149
  const dialogParentDataset = dialogElement.parentElement.dataset
60
150
  if (dialogParentDataset.overlayClick === "overlay_close") return
61
151
 
@@ -69,7 +159,9 @@ export default class PbDialog extends PbEnhancedElement {
69
159
  dialogElement.close()
70
160
  event.stopPropagation()
71
161
  }
72
- })
162
+ }
163
+
164
+ dialogElement.addEventListener("mousedown", dialogElement._outsideClickHandler);
73
165
  })
74
166
  }
75
167
  }