|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * |
| 4 | + * Copyright IBM Corp. 2025, 2025 |
| 5 | + * |
| 6 | + * This source code is licensed under the Apache-2.0 license found in the |
| 7 | + * LICENSE file in the root directory of this source tree. |
| 8 | + */ |
| 9 | + |
| 10 | +import { LitElement, html } from 'lit'; |
| 11 | +import { property, customElement, query } from 'lit/decorators.js'; |
| 12 | +// Carbon components |
| 13 | +import '@carbon/web-components/es/components/stack/index.js'; |
| 14 | +import '@carbon/web-components/es/components/icon-button/index.js'; |
| 15 | +import '@carbon/web-components/es/components/dropdown/index.js'; |
| 16 | +import '@carbon/web-components/es/components/overflow-menu/index.js'; |
| 17 | +// Carbon icons |
| 18 | +import ZoomIn from '@carbon/web-components/es/icons/zoom--in/16.js'; |
| 19 | +import ZoomOut from '@carbon/web-components/es/icons/zoom--out/16.js'; |
| 20 | +import RulerAlt from '@carbon/web-components/es/icons/ruler--alt/16.js'; |
| 21 | +import Pin from '@carbon/web-components/es/icons/pin/16.js'; |
| 22 | +import ColorPalette from '@carbon/web-components/es/icons/color-palette/16.js'; |
| 23 | +import Draggable from '@carbon/web-components/es/icons/draggable/16.js'; |
| 24 | +import TextCreation from '@carbon/web-components/es/icons/text--creation/16.js'; |
| 25 | +import OpenPanelLeft from '@carbon/web-components/es/icons/open-panel--left/16.js'; |
| 26 | +import OpenPanelRight from '@carbon/web-components/es/icons/open-panel--right/16.js'; |
| 27 | +import Move from '@carbon/web-components/es/icons/move/16.js'; |
| 28 | +import Rotate from '@carbon/web-components/es/icons/rotate/16.js'; |
| 29 | + |
| 30 | +import styles from './toolbar.scss?lit'; |
| 31 | + |
| 32 | +/** |
| 33 | + * ToolbarVertical pattern example. |
| 34 | + * |
| 35 | + * @element clabs-toolbar-vertical |
| 36 | + * |
| 37 | + */ |
| 38 | +@customElement('clabs-toolbar-vertical') |
| 39 | +class ToolbarVertical extends LitElement { |
| 40 | + @property({ type: String, reflect: true }) orientation = 'horizontal'; |
| 41 | + @query('cds-stack.toolbar') toolbarEl!: HTMLElement; |
| 42 | + |
| 43 | + /** |
| 44 | + * Lifecycle callback that is called after the element's DOM has been updated the first time. |
| 45 | + */ |
| 46 | + async firstUpdated() { |
| 47 | + this.setAttribute('role', 'toolbar'); |
| 48 | + this.addEventListener('keydown', this._handleKeydown); |
| 49 | + this._initializeFocusableElements(); |
| 50 | + |
| 51 | + await this.updateComplete; |
| 52 | + // Remove dropdown border |
| 53 | + const dropdown = this.renderRoot.querySelector('cds-dropdown'); |
| 54 | + const listBox = dropdown?.shadowRoot?.querySelector('.cds--list-box'); |
| 55 | + if (listBox) { |
| 56 | + (listBox as HTMLElement).style.border = 'none'; |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Lifecycle callback that is called when the element is disconnected from the document's DOM. |
| 62 | + */ |
| 63 | + disconnectedCallback() { |
| 64 | + super.disconnectedCallback(); |
| 65 | + this.removeEventListener('keydown', this._handleKeydown); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Initializes the focusable elements in the toolbar and sets their tabindex. |
| 70 | + */ |
| 71 | + private _initializeFocusableElements() { |
| 72 | + const focusables = this._getFocusableElements(); |
| 73 | + focusables.forEach((el, i) => |
| 74 | + el.setAttribute('tabindex', i === 0 ? '0' : '-1') |
| 75 | + ); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Handles keyboard navigation for toolbar focus management. |
| 80 | + * @param {KeyboardEvent} event The keyboard event. |
| 81 | + */ |
| 82 | + private _handleKeydown(event: KeyboardEvent) { |
| 83 | + const keys = ['ArrowRight', 'ArrowLeft', 'ArrowDown', 'ArrowUp']; |
| 84 | + if (!keys.includes(event.key)) { |
| 85 | + return; |
| 86 | + } |
| 87 | + |
| 88 | + const elements = this._getFocusableElements(); |
| 89 | + |
| 90 | + const current = elements.findIndex( |
| 91 | + (btn) => btn.getAttribute('tabindex') === '0' |
| 92 | + ); |
| 93 | + |
| 94 | + if (current === -1) { |
| 95 | + return; |
| 96 | + } |
| 97 | + |
| 98 | + event.preventDefault(); |
| 99 | + const horizontal = this.orientation === 'horizontal'; |
| 100 | + |
| 101 | + let next = current; |
| 102 | + if (event.key === 'ArrowRight' && horizontal) { |
| 103 | + next = (current + 1) % elements.length; |
| 104 | + } |
| 105 | + if (event.key === 'ArrowLeft' && horizontal) { |
| 106 | + next = (current - 1 + elements.length) % elements.length; |
| 107 | + } |
| 108 | + if (event.key === 'ArrowDown' && !horizontal) { |
| 109 | + next = (current + 1) % elements.length; |
| 110 | + } |
| 111 | + if (event.key === 'ArrowUp' && !horizontal) { |
| 112 | + next = (current - 1 + elements.length) % elements.length; |
| 113 | + } |
| 114 | + |
| 115 | + this._updateTabIndexes(elements, next); |
| 116 | + elements[next].focus(); |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * |
| 121 | + * @param {HTMLElement[]} elements - The list of focusable elements in the toolbar. |
| 122 | + * @param {number} activeIndex - The index of the element to set as active (tabindex=0). |
| 123 | + */ |
| 124 | + private _updateTabIndexes(elements: HTMLElement[], activeIndex: number) { |
| 125 | + elements.forEach((el, i) => |
| 126 | + el.setAttribute('tabindex', i === activeIndex ? '0' : '-1') |
| 127 | + ); |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * Gets all focusable elements within the toolbar. |
| 132 | + * @returns {HTMLElement[]} An array of focusable elements. |
| 133 | + */ |
| 134 | + private _getFocusableElements(): HTMLElement[] { |
| 135 | + return Array.from( |
| 136 | + this.renderRoot.querySelectorAll<HTMLElement>( |
| 137 | + 'cds-icon-button, cds-dropdown, cds-overflow-menu' |
| 138 | + ) |
| 139 | + ); |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * Renders the toolbar template. |
| 144 | + */ |
| 145 | + render() { |
| 146 | + return html` |
| 147 | + <cds-stack class="toolbar" orientation=${this.orientation}> |
| 148 | + <cds-stack class="toolbar-group" orientation=${this.orientation}> |
| 149 | + <cds-icon-button |
| 150 | + kind="ghost" |
| 151 | + enter-delay-ms="100" |
| 152 | + leave-delay-ms="100" |
| 153 | + align=${this.orientation === 'vertical' ? 'right' : 'top'}> |
| 154 | + ${Draggable({ slot: 'icon' })} |
| 155 | + <span slot="tooltip-content">Drag</span> |
| 156 | + </cds-icon-button> |
| 157 | + </cds-stack class="toolbar-group"> |
| 158 | + <cds-stack class="toolbar-group" orientation=${this.orientation}> |
| 159 | + <cds-icon-button |
| 160 | + kind="ghost" |
| 161 | + enter-delay-ms="100" |
| 162 | + leave-delay-ms="100" |
| 163 | + align=${this.orientation === 'vertical' ? 'right' : 'top'}> |
| 164 | + ${RulerAlt({ slot: 'icon' })} |
| 165 | + <span slot="tooltip-content">Ruler</span> |
| 166 | + </cds-icon-button> |
| 167 | + <cds-icon-button |
| 168 | + kind="ghost" |
| 169 | + enter-delay-ms="100" |
| 170 | + leave-delay-ms="100" |
| 171 | + align=${this.orientation === 'vertical' ? 'right' : 'top'}> |
| 172 | + ${Pin({ slot: 'icon' })} |
| 173 | + <span slot="tooltip-content">Pin</span> |
| 174 | + </cds-icon-button> |
| 175 | + <cds-icon-button |
| 176 | + kind="ghost" |
| 177 | + enter-delay-ms="100" |
| 178 | + leave-delay-ms="100" |
| 179 | + caret |
| 180 | + align=${this.orientation === 'vertical' ? 'right' : 'top'}> |
| 181 | + ${ColorPalette({ slot: 'icon' })} |
| 182 | + <span slot="tooltip-content">Color palette</span> |
| 183 | + </cds-icon-button> |
| 184 | + <cds-icon-button |
| 185 | + kind="ghost" |
| 186 | + enter-delay-ms="100" |
| 187 | + leave-delay-ms="100" |
| 188 | + align=${this.orientation === 'vertical' ? 'right' : 'top'}> |
| 189 | + ${TextCreation({ slot: 'icon' })} |
| 190 | + <span slot="tooltip-content">Text creation</span> |
| 191 | + </cds-icon-button> |
| 192 | + </cds-stack class="toolbar-group"> |
| 193 | + <cds-stack class="toolbar-group" orientation=${this.orientation}> |
| 194 | + <cds-icon-button |
| 195 | + kind="ghost" |
| 196 | + enter-delay-ms="100" |
| 197 | + leave-delay-ms="100" |
| 198 | + align=${this.orientation === 'vertical' ? 'right' : 'top'}> |
| 199 | + ${OpenPanelLeft({ slot: 'icon' })} |
| 200 | + <span slot="tooltip-content">Open panel left</span> |
| 201 | + </cds-icon-button> |
| 202 | + <cds-icon-button |
| 203 | + kind="ghost" |
| 204 | + enter-delay-ms="100" |
| 205 | + leave-delay-ms="100" |
| 206 | + align=${this.orientation === 'vertical' ? 'right' : 'top'}> |
| 207 | + ${OpenPanelRight({ slot: 'icon' })} |
| 208 | + <span slot="tooltip-content">Open panel right</span> |
| 209 | + </cds-icon-button> |
| 210 | + </cds-stack class="toolbar-group"> |
| 211 | + <cds-stack class="toolbar-group" orientation=${this.orientation}> |
| 212 | + <cds-icon-button |
| 213 | + kind="ghost" |
| 214 | + enter-delay-ms="100" |
| 215 | + leave-delay-ms="100" |
| 216 | + align=${this.orientation === 'vertical' ? 'right' : 'top'}> |
| 217 | + ${Move({ slot: 'icon' })} |
| 218 | + <span slot="tooltip-content">Move</span> |
| 219 | + </cds-icon-button> |
| 220 | + <cds-icon-button |
| 221 | + kind="ghost" |
| 222 | + enter-delay-ms="100" |
| 223 | + leave-delay-ms="100" |
| 224 | + align=${this.orientation === 'vertical' ? 'right' : 'top'}> |
| 225 | + ${Rotate({ slot: 'icon' })} |
| 226 | + <span slot="tooltip-content">Rotate</span> |
| 227 | + </cds-icon-button> |
| 228 | + </cds-stack class="toolbar-group"> |
| 229 | + <cds-stack class="toolbar-group" orientation=${this.orientation}> |
| 230 | + <cds-icon-button |
| 231 | + kind="ghost" |
| 232 | + enter-delay-ms="100" |
| 233 | + leave-delay-ms="100" |
| 234 | + align=${this.orientation === 'vertical' ? 'right' : 'top'}> |
| 235 | + ${ZoomIn({ slot: 'icon' })} |
| 236 | + <span slot="tooltip-content">Zoom in</span> |
| 237 | + </cds-icon-button> |
| 238 | + <cds-icon-button |
| 239 | + kind="ghost" |
| 240 | + enter-delay-ms="100" |
| 241 | + leave-delay-ms="100" |
| 242 | + align=${this.orientation === 'vertical' ? 'right' : 'top'}> |
| 243 | + ${ZoomOut({ slot: 'icon' })} |
| 244 | + <span slot="tooltip-content">Zoom out</span> |
| 245 | + </cds-icon-button> |
| 246 | + </cds-stack class="toolbar-group"> |
| 247 | + </cds-stack class="toolbar"> |
| 248 | + `; |
| 249 | + } |
| 250 | + |
| 251 | + static styles = styles; |
| 252 | +} |
| 253 | +export default ToolbarVertical; |
0 commit comments