@brightspace-ui/core 3.67.0 → 3.68.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,6 +2,7 @@ import { css, html, LitElement } from 'lit';
2
2
 
3
3
  /**
4
4
  * A button container component for button toggles.
5
+ * @fires d2l-button-toggle-before-change - Dispatched before pressed state is updated. Can be canceled to allow the consumer to handle toggling pressed state. This is useful if something needs to happen prior to the pressed state being toggled.
5
6
  */
6
7
  class ButtonToggle extends LitElement {
7
8
 
@@ -79,9 +80,16 @@ class ButtonToggle extends LitElement {
79
80
  }
80
81
 
81
82
  async _handleClick(pressed) {
82
- this.pressed = pressed;
83
- await this.updateComplete;
84
- this.focus();
83
+ const beforeToggleEvent = new CustomEvent('d2l-button-toggle-before-change', {
84
+ detail: {
85
+ update: (newPressed) => this._updatePressedState(newPressed)
86
+ },
87
+ cancelable: true
88
+ });
89
+ this.dispatchEvent(beforeToggleEvent);
90
+ if (beforeToggleEvent.defaultPrevented) return;
91
+
92
+ this._updatePressedState(pressed);
85
93
  }
86
94
 
87
95
  _handleNotPressedClick() {
@@ -92,6 +100,12 @@ class ButtonToggle extends LitElement {
92
100
  this._handleClick(false);
93
101
  }
94
102
 
103
+ async _updatePressedState(newPressed) {
104
+ this.pressed = newPressed;
105
+ await this.updateComplete;
106
+ this.focus();
107
+ }
108
+
95
109
  }
96
110
 
97
111
  customElements.define('d2l-button-toggle', ButtonToggle);
@@ -54,6 +54,26 @@
54
54
  </template>
55
55
  </d2l-demo-snippet>
56
56
 
57
+ <h2>Toggle Button (with consumer specifying when to toggle)</h2>
58
+
59
+ <d2l-demo-snippet>
60
+ <template>
61
+ <d2l-button-toggle id="toggle-button-icon-promise-delay">
62
+ <d2l-button-icon slot="not-pressed" icon="tier1:pin-hollow" text="Unpinned, click to pin."></d2l-button-icon>
63
+ <d2l-button-icon slot="pressed" icon="tier1:pin-filled" text="Pinned, click to unpin."></d2l-button-icon>
64
+ </d2l-button-toggle>
65
+ <script>
66
+ document.querySelector('#toggle-button-icon-promise-delay').addEventListener('d2l-button-toggle-before-change', e => {
67
+ e.preventDefault();
68
+ setTimeout(() => {
69
+ e.detail.update(!e.target.pressed);
70
+ }, 2000);
71
+ });
72
+ document.querySelector('#toggle-button-icon-promise-delay').addEventListener('d2l-button-toggle-change', e => console.log(e));
73
+ </script>
74
+ </template>
75
+ </d2l-demo-snippet>
76
+
57
77
  </d2l-demo-page>
58
78
 
59
79
  </body>
@@ -739,6 +739,10 @@
739
739
  }
740
740
  ],
741
741
  "events": [
742
+ {
743
+ "name": "d2l-button-toggle-before-change",
744
+ "description": "Dispatched before pressed state is updated. Can be canceled to allow the consumer to handle toggling pressed state. This is useful if something needs to happen prior to the pressed state being toggled."
745
+ },
742
746
  {
743
747
  "name": "d2l-button-toggle-change",
744
748
  "description": "Dispatched when the pressed state changes"
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@brightspace-ui/core",
3
- "version": "3.67.0",
3
+ "version": "3.68.0",
4
4
  "description": "A collection of accessible, free, open-source web components for building Brightspace applications",
5
5
  "type": "module",
6
6
  "repository": "https://github.com/BrightspaceUI/core.git",