|
| 1 | +import clsx from "clsx"; |
| 2 | +import { css, html, type PropertyValues } from "lit"; |
| 3 | +import { |
| 4 | + customElement, |
| 5 | + property, |
| 6 | + query, |
| 7 | + queryAll, |
| 8 | + state, |
| 9 | +} from "lit/decorators.js"; |
| 10 | +import { ifDefined } from "lit/directives/if-defined.js"; |
| 11 | +import debounce from "lodash/fp/debounce"; |
| 12 | + |
| 13 | +import { TailwindElement } from "@/classes/TailwindElement"; |
| 14 | +import type { Tag } from "@/components/ui/tag"; |
| 15 | +import type { UnderlyingFunction } from "@/types/utils"; |
| 16 | +import localize from "@/utils/localize"; |
| 17 | +import { tw } from "@/utils/tailwind"; |
| 18 | + |
| 19 | +/** |
| 20 | + * Displays all the tags that can be contained to one line. |
| 21 | + * Overflowing tags are displayed in a popover. |
| 22 | + * |
| 23 | + * @cssproperty width |
| 24 | + */ |
| 25 | +@customElement("btrix-tag-container") |
| 26 | +export class TagContainer extends TailwindElement { |
| 27 | + static styles = css` |
| 28 | + :host { |
| 29 | + --width: 100%; |
| 30 | + } |
| 31 | + `; |
| 32 | + |
| 33 | + @property({ type: Array }) |
| 34 | + tags: string[] = []; |
| 35 | + |
| 36 | + @query("#container") |
| 37 | + private readonly container?: HTMLElement | null; |
| 38 | + |
| 39 | + @queryAll("btrix-tag") |
| 40 | + private readonly tagNodes!: NodeListOf<Tag>; |
| 41 | + |
| 42 | + @state() |
| 43 | + private displayLimit?: number; |
| 44 | + |
| 45 | + disconnectedCallback(): void { |
| 46 | + this.debouncedCalculate.cancel(); |
| 47 | + super.disconnectedCallback(); |
| 48 | + } |
| 49 | + |
| 50 | + protected updated(changedProperties: PropertyValues): void { |
| 51 | + if (changedProperties.get("tags")) { |
| 52 | + this.debouncedCalculate.cancel(); |
| 53 | + this.calculate(); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + render() { |
| 58 | + const maxTags = this.tags.length; |
| 59 | + const displayLimit = this.displayLimit; |
| 60 | + const remainder = displayLimit && maxTags - displayLimit; |
| 61 | + |
| 62 | + return html` |
| 63 | + <sl-resize-observer |
| 64 | + @sl-resize=${this.debouncedCalculate as UnderlyingFunction< |
| 65 | + typeof this.calculate |
| 66 | + >} |
| 67 | + > |
| 68 | + <div class="flex items-center"> |
| 69 | + <div |
| 70 | + id="container" |
| 71 | + class="flex h-6 w-[var(--width)] flex-wrap gap-x-1.5 overflow-hidden contain-content" |
| 72 | + > |
| 73 | + ${this.tags.map( |
| 74 | + (tag, i) => |
| 75 | + html`<btrix-tag |
| 76 | + aria-hidden=${ifDefined( |
| 77 | + displayLimit === undefined |
| 78 | + ? undefined |
| 79 | + : i > displayLimit - 1 |
| 80 | + ? "true" |
| 81 | + : "false", |
| 82 | + )} |
| 83 | + >${tag}</btrix-tag |
| 84 | + >`, |
| 85 | + )} |
| 86 | + </div> |
| 87 | +
|
| 88 | + <btrix-popover hoist placement="right"> |
| 89 | + <btrix-badge |
| 90 | + variant="text" |
| 91 | + size="large" |
| 92 | + class=${clsx(!remainder && tw`invisible`)} |
| 93 | + aria-hidden=${remainder ? "false" : "true"} |
| 94 | + tabIndex="0" |
| 95 | + >+${localize.number(remainder || maxTags)}</btrix-badge |
| 96 | + > |
| 97 | + <div slot="content" class="z-50 flex flex-wrap gap-1.5"> |
| 98 | + ${this.tags |
| 99 | + .slice(displayLimit) |
| 100 | + .map((tag) => html`<btrix-tag>${tag}</btrix-tag>`)} |
| 101 | + </div> |
| 102 | + </btrix-popover> |
| 103 | + </div> |
| 104 | + </sl-resize-observer> |
| 105 | + `; |
| 106 | + } |
| 107 | + |
| 108 | + private readonly calculate = () => { |
| 109 | + const tagNodes = Array.from(this.tagNodes); |
| 110 | + |
| 111 | + if (!tagNodes.length || !this.container) return; |
| 112 | + |
| 113 | + const containerRect = this.container.getBoundingClientRect(); |
| 114 | + const containerTop = containerRect.top; |
| 115 | + |
| 116 | + // Reset width |
| 117 | + this.style.setProperty("--width", "100%"); |
| 118 | + const idx = tagNodes.findIndex( |
| 119 | + (el) => el.getBoundingClientRect().top > containerTop, |
| 120 | + ); |
| 121 | + |
| 122 | + if (idx === -1) return; |
| 123 | + const lastVisible = tagNodes[idx - 1]; |
| 124 | + if (lastVisible as unknown) { |
| 125 | + const rect = lastVisible.getBoundingClientRect(); |
| 126 | + // Decrease width of container to match end of last visible tag |
| 127 | + this.style.setProperty( |
| 128 | + "--width", |
| 129 | + `${rect.left - containerRect.left + rect.width}px`, |
| 130 | + ); |
| 131 | + } |
| 132 | + |
| 133 | + this.displayLimit = idx; |
| 134 | + }; |
| 135 | + |
| 136 | + private readonly debouncedCalculate = debounce(50)(this.calculate); |
| 137 | +} |
0 commit comments