{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "truncated-text",
  "type": "registry:ui",
  "title": "<ui-truncated-text>",
  "description": "Carbon for IBM Products `TruncatedText` (`c4p-truncated-text`): `value` cut to `lines` lines with an ellipsis. Only when it doesn't fit: `type=\"tooltip\"` makes it a button that shows the whole text in a tooltip (and is named by it); `type=\"expand\"` adds a \"View more\" / \"View less\" link-coloured toggle under it that opens the rest, animating the height (110ms, none with reduced motion). The text takes the font of where it sits. Copied from @purpurabog/ui 2.3.0; the package is the maintained source, this copy is yours to change. Icons from the Carbon Design System (Apache-2.0, © IBM Corp.; see THIRD-PARTY-NOTICES.md in the package).",
  "dependencies": [
    "lit@^3.3.0"
  ],
  "registryDependencies": [
    "@kanok-ui/core-motion",
    "@kanok-ui/core-position",
    "@kanok-ui/shared-align",
    "@kanok-ui/shared-element",
    "@kanok-ui/shared-events",
    "@kanok-ui/shared-field",
    "@kanok-ui/shared-localize",
    "@kanok-ui/tooltip",
    "@kanok-ui/tokens"
  ],
  "files": [
    {
      "path": "src/primitives/truncated-text/truncated-text.ts",
      "type": "registry:ui",
      "target": "components/ui/primitives/truncated-text/truncated-text.ts",
      "content": "import { html, css, type PropertyValues } from 'lit';\nimport { customElement, property, query, state } from 'lit/decorators.js';\nimport { styleMap } from 'lit/directives/style-map.js';\nimport type { Placement } from '../../core/position.ts';\nimport { cssDuration } from '../../core/motion.ts';\nimport type { ExpandedDetail } from '../shared/events.ts';\nimport { LocalizeController } from '../shared/localize.ts';\nimport { uid } from '../shared/field.ts';\nimport { WithAlign } from '../shared/align.ts';\nimport { UiElement } from '../shared/element.ts';\nimport '../tooltip/tooltip.ts';\n\nexport type TruncatedTextType = 'tooltip' | 'expand';\n\n/**\n * Carbon for IBM Products `TruncatedText` (`c4p-truncated-text`): `value` cut to `lines` lines with an ellipsis. Only\n * when it doesn't fit: `type=\"tooltip\"` makes it a button that shows the whole text in a tooltip (and is named by it);\n * `type=\"expand\"` adds a \"View more\" / \"View less\" link-coloured toggle under it that opens the rest, animating the\n * height (110ms, none with reduced motion). The text takes the font of where it sits.\n *\n * State on the host for styling: `data-overflowing` while the text doesn't fit its `lines` (kept while expanded).\n *\n * @cssstate overflowing - While the text doesn't fit its `lines` (also `[data-overflowing]`)\n * @fires ui-toggle - detail: { expanded } when the toggle opens or closes the text\n * @csspart content - The text\n * @csspart trigger - The button around the text of a truncated `type=\"tooltip\"`\n * @csspart toggle - The expand / collapse button\n * @attr align - Carbon's name for `placement` (the tooltip's side)\n * @cssprop --ui-focus-offset - `outline-offset` of its focus ring (default -2px: inside; Kanok: 2px outside)\n */\n@customElement('ui-truncated-text')\nexport class UiTruncatedText extends WithAlign(UiElement) {\n    static override hostStates: readonly string[] = ['overflowing'];\n\n    static override readonly styles = css`\n        :host {\n            display: block;\n            inline-size: 100%;\n        }\n\n        .content {\n            display: -webkit-box;\n            overflow: hidden;\n            -webkit-box-orient: vertical;\n            -webkit-line-clamp: var(--_lines);\n            text-overflow: clip;\n            white-space: normal;\n        }\n\n        ui-tooltip {\n            display: block;\n        }\n\n        .trigger {\n            display: block;\n            inline-size: 100%;\n            padding: 0;\n            border: 0;\n            background: none;\n            color: inherit;\n            cursor: default;\n            font: inherit;\n            letter-spacing: inherit;\n            text-align: inherit;\n        }\n\n        .toggle {\n            padding: 0 2px;\n            border: none;\n            background-color: transparent;\n            color: var(--ui-link);\n            cursor: pointer;\n            font: inherit;\n            letter-spacing: inherit;\n            line-height: inherit;\n        }\n\n        .trigger:focus-visible,\n        .toggle:focus-visible {\n            outline: var(--ui-focus-width, 2px) solid var(--ui-focus);\n            outline-offset: var(--ui-focus-offset, -2px);\n            border-radius: var(--ui-link-focus-radius, 0); /* as a link's ring (Kanok: 4px) */\n        }\n    `;\n\n    /** The text. */\n    @property() value = '';\n\n    /** Lines shown before the ellipsis; 0 = all. */\n    @property({ type: Number }) lines = 2;\n\n    /** What a truncated text offers: the whole text in a tooltip, or a toggle that expands it. */\n    @property({ reflect: true }) type: TruncatedTextType = 'tooltip';\n\n    /** The tooltip's side. */\n    @property({ reflect: true }) placement: Placement = 'top';\n\n    /** The expand toggle's text (default \"View more\", translated). */\n    @property({ attribute: 'expand-label' }) expandLabel = '';\n\n    /** The collapse toggle's text (default \"View less\", translated). */\n    @property({ attribute: 'collapse-label' }) collapseLabel = '';\n\n    /** Whether an expandable text shows in full. */\n    @property({ type: Boolean, reflect: true }) expanded = false;\n\n    @state() private overflowing = false;\n\n    /** Collapsing: the clamp waits for the height animation, so the text stays visible while it shrinks (Carbon). */\n    @state() private closing = false;\n\n    @query('.content') private readonly content?: HTMLElement;\n\n    private readonly localize = new LocalizeController(this);\n    private readonly contentId = uid('ui-truncated-text');\n    private resizeObserver?: ResizeObserver;\n\n    private animation?: Animation;\n\n    /** The clamped text's height, the end of the collapse animation. */\n    private clampedHeight = 0;\n\n    override connectedCallback() {\n        super.connectedCallback();\n        if (typeof ResizeObserver === 'undefined') return;\n        this.resizeObserver = new ResizeObserver(() => this.measure());\n        this.resizeObserver.observe(this);\n    }\n\n    override disconnectedCallback() {\n        super.disconnectedCallback();\n        this.resizeObserver?.disconnect();\n        // a running animation holds its element\n        this.animation?.cancel();\n        this.closing = false;\n    }\n\n    protected override updated(changed: PropertyValues<this>) {\n        super.updated(changed);\n        if (changed.has('value') || changed.has('lines') || changed.has('type')) this.measure();\n        this.toggleAttribute('data-overflowing', this.overflowing);\n    }\n\n    /** Whether the clamped text hides anything (kept while expanded: the full text no longer tells). */\n    private measure() {\n        const el = this.content;\n        if (!el || this.expanded || this.closing) return;\n        this.read(el);\n    }\n\n    private read(el: HTMLElement) {\n        this.clampedHeight = el.clientHeight;\n        this.overflowing = this.lines > 0 && el.scrollHeight - el.clientHeight > 1;\n    }\n\n    /** `measure` for an open text, under a moment's clamp: it started expanded, or its box changed while open. */\n    private measureOpen(el: HTMLElement) {\n        const open = el.style.getPropertyValue('--_lines');\n        el.style.setProperty('--_lines', String(this.lines));\n        this.read(el);\n        el.style.setProperty('--_lines', open);\n    }\n\n    private duration() {\n        return cssDuration(getComputedStyle(this).getPropertyValue('--ui-duration-moderate'));\n    }\n\n    private async toggle() {\n        const el = this.content;\n        if (!el) return;\n        const from = el.clientHeight;\n        const expanding = !this.expanded;\n        if (expanding) this.clampedHeight = from;\n        else this.measureOpen(el);\n        this.closing = !expanding;\n        this.expanded = expanding;\n        this.dispatchEvent(new CustomEvent<ExpandedDetail>('ui-toggle', { detail: { expanded: expanding }, bubbles: true, composed: true }));\n        await this.updateComplete;\n        const to = expanding ? el.scrollHeight : this.clampedHeight;\n        const duration = this.duration();\n        if (duration > 0 && typeof el.animate === 'function') {\n            this.animation?.cancel();\n            this.animation = el.animate([{ maxBlockSize: `${from}px` }, { maxBlockSize: `${to}px` }], { duration, easing: 'cubic-bezier(0.2, 0, 0.38, 0.9)' });\n            // a timer, not animation.finished: animations stand still in a hidden page, and the clamp must come back\n            await new Promise((resolve) => setTimeout(resolve, duration));\n        }\n        this.closing = false;\n    }\n\n    private renderContent() {\n        const clamp = this.lines > 0 && !this.expanded && !this.closing ? String(this.lines) : 'none';\n        return html`<div id=${this.contentId} class=\"content\" part=\"content\" style=${styleMap({ '--_lines': clamp })}>${this.value}</div>`;\n    }\n\n    override render() {\n        if (this.type === 'tooltip') {\n            if (!this.overflowing) return this.renderContent();\n            return html`<ui-tooltip text=${this.value} as-label placement=${this.placement}><button type=\"button\" class=\"trigger\" part=\"trigger\">${this.renderContent()}</button></ui-tooltip>`;\n        }\n        const { viewMore, viewLess } = this.localize.t.common;\n        const label = this.expanded ? this.collapseLabel || viewLess : this.expandLabel || viewMore;\n        const toggle = html`<button type=\"button\" class=\"toggle\" part=\"toggle\" aria-expanded=${this.expanded ? 'true' : 'false'} aria-controls=${this.contentId} @click=${this.toggle}>${label}</button>`;\n        return html`${this.renderContent()}${this.overflowing || this.expanded ? toggle : ''}`;\n    }\n}\n\nexport interface UiTruncatedTextEventMap extends HTMLElementEventMap {\n    'ui-toggle': CustomEvent<ExpandedDetail>;\n}\n\nexport interface UiTruncatedText {\n    addEventListener<K extends keyof UiTruncatedTextEventMap>(type: K, listener: (this: UiTruncatedText, event: UiTruncatedTextEventMap[K]) => unknown, options?: boolean | AddEventListenerOptions): void;\n    addEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions): void;\n    removeEventListener<K extends keyof UiTruncatedTextEventMap>(type: K, listener: (this: UiTruncatedText, event: UiTruncatedTextEventMap[K]) => unknown, options?: boolean | EventListenerOptions): void;\n    removeEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | EventListenerOptions): void;\n}\n\ndeclare global {\n    interface HTMLElementTagNameMap {\n        'ui-truncated-text': UiTruncatedText;\n    }\n}\n"
    }
  ]
}
