|
| 1 | +/* |
| 2 | +Copyright 2021 Adobe. All rights reserved. |
| 3 | +This file is licensed to you under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. You may obtain a copy |
| 5 | +of the License at http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +
|
| 7 | +Unless required by applicable law or agreed to in writing, software distributed under |
| 8 | +the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS |
| 9 | +OF ANY KIND, either express or implied. See the License for the specific language |
| 10 | +governing permissions and limitations under the License. |
| 11 | +*/ |
| 12 | + |
| 13 | +import { |
| 14 | + CSSResultArray, |
| 15 | + html, |
| 16 | + PropertyValues, |
| 17 | + SpectrumElement, |
| 18 | + TemplateResult, |
| 19 | +} from '@spectrum-web-components/base'; |
| 20 | +import type { Overlay, Placement } from '@spectrum-web-components/overlay'; |
| 21 | +import '@spectrum-web-components/overlay/sp-overlay.js'; |
| 22 | +import '@spectrum-web-components/tooltip/sp-tooltip.js'; |
| 23 | +import { |
| 24 | + property, |
| 25 | + query, |
| 26 | + queryAssignedElements, |
| 27 | + queryAssignedNodes, |
| 28 | + state, |
| 29 | +} from '@spectrum-web-components/base/src/decorators.js'; |
| 30 | + |
| 31 | +import styles from './truncated.css.js'; |
| 32 | + |
| 33 | +/** |
| 34 | + * @element sp-truncated |
| 35 | + */ |
| 36 | +export class Truncated extends SpectrumElement { |
| 37 | + public static override get styles(): CSSResultArray { |
| 38 | + return [styles]; |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * @type {"top" | "top-start" | "top-end" | "right" | "right-start" | "right-end" | "bottom" | "bottom-start" | "bottom-end" | "left" | "left-start" | "left-end"} |
| 43 | + */ |
| 44 | + @property() |
| 45 | + placement: Placement = 'top-start'; |
| 46 | + |
| 47 | + /* |
| 48 | + * @type {String} |
| 49 | + * @attr success-message |
| 50 | + * @description The message to display when the text is copied to the clipboard after clicking on the truncated text |
| 51 | + */ |
| 52 | + @property({ type: String, attribute: 'success-message' }) |
| 53 | + successMessage = 'Copied to clipboard'; |
| 54 | + |
| 55 | + @state() |
| 56 | + hasCopied = false; |
| 57 | + |
| 58 | + @state() |
| 59 | + private fullText = ''; |
| 60 | + |
| 61 | + @state() |
| 62 | + private overflowing = false; |
| 63 | + |
| 64 | + @query('#content') |
| 65 | + private content!: HTMLElement; |
| 66 | + |
| 67 | + @query('#overlay') |
| 68 | + private overlayEl?: Overlay; |
| 69 | + |
| 70 | + @queryAssignedNodes({ flatten: true }) |
| 71 | + private slottedContent!: Node[]; |
| 72 | + |
| 73 | + // elements instead of nodes because, according to spec, |
| 74 | + // flattened assignedNodes will return a slot's *children* if there are no assigned nodes. |
| 75 | + // ¯\_(ツ)_/¯ |
| 76 | + @queryAssignedElements({ slot: 'overflow', flatten: true }) |
| 77 | + private slottedOverflow!: HTMLElement[]; |
| 78 | + |
| 79 | + get hasCustomOverflow(): boolean { |
| 80 | + return this.slottedOverflow.length > 0; |
| 81 | + } |
| 82 | + |
| 83 | + private resizeObserver = new ResizeObserver(() => { |
| 84 | + this.measureOverflow(); |
| 85 | + }); |
| 86 | + |
| 87 | + private mutationObserver = new MutationObserver(() => { |
| 88 | + this.copyText(); |
| 89 | + }); |
| 90 | + |
| 91 | + override render(): TemplateResult { |
| 92 | + /* eslint-disable lit-a11y/click-events-have-key-events */ |
| 93 | + return html` |
| 94 | + <span id="content" @click=${this.handleClick}> |
| 95 | + <slot></slot> |
| 96 | + </span> |
| 97 | + ${this.renderTooltip()} |
| 98 | + `; |
| 99 | + /* eslint-enable lit-a11y/click-events-have-key-events */ |
| 100 | + } |
| 101 | + |
| 102 | + private renderTooltip(): TemplateResult | undefined { |
| 103 | + if (!this.overflowing) { |
| 104 | + return html` |
| 105 | + <slot |
| 106 | + name="overflow" |
| 107 | + style="display: none" |
| 108 | + @slotchange=${this.handleOverflowSlotchange} |
| 109 | + ></slot> |
| 110 | + `; |
| 111 | + } |
| 112 | + return html` |
| 113 | + <sp-overlay |
| 114 | + id="overlay" |
| 115 | + .triggerElement=${this as HTMLElement} |
| 116 | + .triggerInteraction=${'hover'} |
| 117 | + type="hint" |
| 118 | + placement=${this.placement} |
| 119 | + > |
| 120 | + <sp-tooltip name="tooltip"> |
| 121 | + ${!this.hasCopied |
| 122 | + ? html` |
| 123 | + <slot |
| 124 | + name="overflow" |
| 125 | + @slotchange=${this.handleOverflowSlotchange} |
| 126 | + > |
| 127 | + ${this.fullText} |
| 128 | + </slot> |
| 129 | + ` |
| 130 | + : this.successMessage} |
| 131 | + </sp-tooltip> |
| 132 | + </sp-overlay> |
| 133 | + `; |
| 134 | + } |
| 135 | + |
| 136 | + protected override firstUpdated( |
| 137 | + _changedProperties: PropertyValues<this> |
| 138 | + ): void { |
| 139 | + this.resizeObserver.observe(this); |
| 140 | + this.resizeObserver.observe(this.content); |
| 141 | + this.copyText(); |
| 142 | + this.measureOverflow(); |
| 143 | + } |
| 144 | + |
| 145 | + protected override updated(changedProperties: PropertyValues<this>): void { |
| 146 | + super.updated(changedProperties); |
| 147 | + if ( |
| 148 | + changedProperties.has('hasCopied') && |
| 149 | + this.hasCopied && |
| 150 | + this.overlayEl |
| 151 | + ) { |
| 152 | + // we know overlayEl exists because it couldn't copy the text otherwise |
| 153 | + this.overlayEl.open = true; |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + private handleOverflowSlotchange(): void { |
| 158 | + this.mutationObserver.disconnect(); |
| 159 | + if (!this.hasCustomOverflow) { |
| 160 | + /* c8 ignore next 5 */ |
| 161 | + this.mutationObserver.observe(this.content, { |
| 162 | + subtree: true, |
| 163 | + childList: true, |
| 164 | + characterData: true, |
| 165 | + }); |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + private handleClick(): void { |
| 170 | + if (!this.overflowing) return; |
| 171 | + |
| 172 | + const textToCopy = this.slottedContent |
| 173 | + .map((node) => node.textContent ?? '') |
| 174 | + .join('') |
| 175 | + .trim(); |
| 176 | + navigator.clipboard.writeText(textToCopy); |
| 177 | + this.hasCopied = true; |
| 178 | + setTimeout(() => { |
| 179 | + this.hasCopied = false; |
| 180 | + }, 6000); |
| 181 | + } |
| 182 | + |
| 183 | + private measureOverflow(): void { |
| 184 | + // Add 1 because Safari sometimes rounds by 1px, breaking the calculation otherwise |
| 185 | + this.overflowing = this.content.offsetWidth > this.clientWidth + 1; |
| 186 | + } |
| 187 | + |
| 188 | + // Copies just the textContent of slotted nodes into the tooltip to avoid duplicating the user's DOM |
| 189 | + private copyText(): void { |
| 190 | + if (this.hasCustomOverflow) return; |
| 191 | + this.fullText = this.slottedContent |
| 192 | + .map((node) => node.textContent ?? '') |
| 193 | + .join(''); |
| 194 | + } |
| 195 | +} |
0 commit comments