{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "edit-in-place",
  "type": "registry:ui",
  "title": "<ui-edit-in-place>",
  "description": "Carbon for IBM Products `EditInPlace`: a value shown as plain text that is edited right where it is. Hovered, it gets the field fill and an \"Edit\" button; focused (a click, Tab), it is a field with \"Cancel\" and \"Save\" buttons (enabled once the value changed; a valid change can be saved). Enter saves and keeps editing (with nothing to save it stops editing, the focus staying); Escape puts the value back; leaving it saves a valid change and otherwise puts the value back. `invalid` draws the error border, a warning icon and `invalid-text` (described by the field); `readonly` shows \"Edit off\", whose high-contrast popover says why. Sizes sm 40 / md 48 / lg 64. 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-edit-in-place",
    "@kanok-ui/icon-button",
    "@kanok-ui/popover",
    "@kanok-ui/shared-element",
    "@kanok-ui/shared-events",
    "@kanok-ui/shared-field",
    "@kanok-ui/shared-localize",
    "@kanok-ui/shared-position",
    "@kanok-ui/tokens"
  ],
  "files": [
    {
      "path": "src/primitives/edit-in-place/edit-in-place.ts",
      "type": "registry:ui",
      "target": "components/ui/primitives/edit-in-place/edit-in-place.ts",
      "content": "import { html, css, nothing, svg } from 'lit';\nimport { customElement, property, query, state } from 'lit/decorators.js';\nimport { LocalizeController } from '../shared/localize.ts';\nimport { UiElement } from '../shared/element.ts';\nimport { uid } from '../shared/field.ts';\nimport type { Placement } from '../shared/position.ts';\nimport { editKeyAction, editState, leaveAction, type EditInPlaceSize } from '../../core/edit-in-place.ts';\nimport '../icon-button/icon-button.ts';\nimport '../popover/popover.ts';\nimport { isOwnEvent } from '../shared/events.ts';\n\nexport type { EditInPlaceSize } from '../../core/edit-in-place.ts';\n\n// Carbon icons (16px): Edit, EditOff, Close, Checkmark, WarningFilled\nconst editIcon = svg`<svg viewBox=\"0 0 32 32\" aria-hidden=\"true\"><path d=\"M2 26H30V28H2z\" /><path d=\"M25.4 9c.8-.8.8-2 0-2.8l-3.6-3.6c-.8-.8-2-.8-2.8 0l-15 15V24h6.4L25.4 9zM20.4 4 24 7.6l-3 3L17.4 7l3-3zM6 22v-3.6l10-10 3.6 3.6-10 10H6z\" /></svg>`;\nconst editOffIcon = svg`<svg viewBox=\"0 0 32 32\" aria-hidden=\"true\"><path d=\"M30 28.6 3.4 2 2 3.4l10.1 10.1L4 21.6V28h6.4l8.1-8.1L28.6 30l1.4-1.4zM9.6 26H6v-3.6l7.5-7.5 3.6 3.6L9.6 26z\" /><path d=\"M29.4 6.2l-3.6-3.6c-.8-.8-2-.8-2.8 0l-8 8 1.4 1.4L20 8.4l3.6 3.6-3.6 3.6 1.4 1.4 8-8c.8-.8.8-2 0-2.8zM25 10.6 21.4 7l3-3L28 7.6l-3 3z\" /></svg>`;\nconst closeIcon = svg`<svg viewBox=\"0 0 32 32\" aria-hidden=\"true\"><path d=\"M17.414 16 24 9.414 22.586 8 16 14.586 9.414 8 8 9.414 14.586 16 8 22.586 9.414 24 16 17.414 22.586 24 24 22.586 17.414 16z\" /></svg>`;\nconst checkIcon = svg`<svg viewBox=\"0 0 32 32\" aria-hidden=\"true\"><path d=\"M13 24 4 15l1.414-1.414L13 21.171 26.586 7.586 28 9 13 24z\" /></svg>`;\nconst warningIcon = svg`<svg class=\"warning\" viewBox=\"0 0 16 16\" aria-hidden=\"true\"><path d=\"M8 1C4.2 1 1 4.2 1 8s3.2 7 7 7 7-3.1 7-7-3.1-7-7-7zm-.5 3h1v5h-1V4zm.5 8.2c-.4 0-.8-.4-.8-.8s.3-.8.8-.8c.4 0 .8.4.8.8s-.4.8-.8.8z\" /></svg>`;\n\n/**\n * Carbon for IBM Products `EditInPlace`: a value shown as plain text that is edited right where it is. Hovered, it\n * gets the field fill and an \"Edit\" button; focused (a click, Tab), it is a field with \"Cancel\" and \"Save\" buttons\n * (enabled once the value changed; a valid change can be saved). Enter saves and keeps editing (with nothing to save it\n * stops editing, the focus staying); Escape puts the value back; leaving it saves a valid change and otherwise puts the\n * value back. `invalid` draws the error border, a warning icon and `invalid-text` (described by the field);\n * `readonly` shows \"Edit off\", whose high-contrast popover says why. Sizes sm 40 / md 48 / lg 64.\n *\n * State on the host for styling: `data-editing` while it is a field (editing).\n *\n * @cssstate editing - While it is a field (editing) (also `[data-editing]`)\n * @fires input - On every keystroke (`value` already updated)\n * @fires ui-save - detail: { value } a change was saved (Save, Enter, leaving it)\n * @fires ui-cancel - detail: { value } the value was put back (Cancel, Escape, leaving it without a valid change)\n * @csspart base - The field box\n * @csspart input - The `<input>`\n * @csspart actions - The buttons' box\n * @csspart invalid-text - The error message\n */\n@customElement('ui-edit-in-place')\nexport class UiEditInPlace extends UiElement {\n    static override hostStates: readonly string[] = ['editing'];\n\n    static override readonly styles = css`\n        :host {\n            display: block;\n        }\n\n        :host([hidden]) {\n            display: none;\n        }\n\n        .container {\n            --_size: 40px;\n\n            display: flex;\n            align-items: center;\n            box-sizing: border-box;\n            min-block-size: var(--_size);\n            background: transparent;\n            cursor: pointer;\n        }\n\n        :host([size='md']) .container {\n            --_size: 48px;\n        }\n\n        :host([size='lg']) .container {\n            --_size: 64px;\n        }\n\n        /* Carbon: field-01 under the pointer and while editing; the focus line around it while the field has focus */\n        .container:hover,\n        .container.editing {\n            background: var(--ui-field-1);\n        }\n\n        .container.editing:has(input:focus) {\n            outline: var(--ui-focus-width, 2px) solid var(--ui-focus);\n            outline-offset: 0;\n            border-radius: var(--ui-field-radius, 0); /* a field's corners while it is one (Kanok: 6px) */\n        }\n\n        .container.invalid {\n            border: 2px solid var(--ui-error);\n        }\n\n        input {\n            flex: 1;\n            min-inline-size: 0;\n            box-sizing: border-box;\n            block-size: 40px;\n            margin: 0;\n            padding: 0 var(--ui-space-md);\n            border: 0;\n            background: transparent;\n            color: var(--ui-ink);\n            font: var(--ui-font-body-sm);\n            font-size: var(--ui-field-font-size, 14px);\n            line-height: 18px; /* body-compact-01 */\n            letter-spacing: var(--ui-tracking-body);\n            cursor: pointer;\n            outline: none;\n        }\n\n        input::placeholder {\n            color: var(--ui-ink-placeholder);\n            opacity: 1;\n        }\n\n        :host([inherit-typography]) input {\n            font-size: inherit;\n            font-weight: inherit;\n            line-height: inherit;\n            letter-spacing: inherit;\n        }\n\n        /* the buttons: squares of the height; the box opens to two of them while editing (Carbon slide-in, 150ms) */\n        .actions {\n            --ui-field-height-lg: var(--_size);\n            --_width: var(--_size);\n            --_editing-width: calc(var(--_size) * 2);\n\n            display: inline-flex;\n            flex-shrink: 0;\n            align-items: center;\n            align-self: stretch;\n            justify-content: flex-end;\n            inline-size: var(--_width);\n        }\n\n        .invalid .actions,\n        .editing.has-error .actions {\n            --_width: calc(var(--_size) + 32px);\n            --_editing-width: calc(var(--_size) * 2 + 32px);\n        }\n\n        .editing:not(.readonly) .actions {\n            inline-size: var(--_editing-width);\n            animation: open var(--ui-duration-moderate-01) var(--ui-easing-entrance);\n        }\n\n        @keyframes open {\n            0% {\n                overflow: hidden;\n                inline-size: var(--_width);\n            }\n\n            99% {\n                overflow: hidden;\n                inline-size: var(--_editing-width);\n            }\n\n            100% {\n                overflow: visible;\n                inline-size: var(--_editing-width);\n            }\n        }\n\n        @media (prefers-reduced-motion: reduce) {\n            .editing:not(.readonly) .actions {\n                animation: none;\n            }\n        }\n\n        .edit {\n            visibility: hidden;\n        }\n\n        .container:hover .edit,\n        :host([edit-always-visible]) .edit {\n            visibility: visible;\n        }\n\n        .warning {\n            flex-shrink: 0;\n            inline-size: 16px;\n            block-size: 16px;\n            margin: auto var(--ui-space-xs);\n            fill: var(--ui-error);\n        }\n\n        /* text-error, as every field's error line (Carbon's EditInPlace paints it support-error, short of 4.5:1 on dark layers) */\n        .invalid-text {\n            margin: var(--ui-space-xs) 0 0;\n            color: var(--ui-error-text, var(--ui-error));\n            font: var(--ui-font-caption);\n            line-height: 16px; /* label-01: 12 × 1.33333 */\n            letter-spacing: var(--ui-tracking-caption);\n        }\n\n        .note {\n            margin: 0;\n        }\n\n        ui-icon-button svg {\n            width: 16px;\n            height: 16px;\n            fill: currentColor;\n        }\n\n        @media (forced-colors: active) {\n            .container.editing:has(input:focus) {\n                outline-color: Highlight;\n            }\n\n            .warning {\n                fill: CanvasText;\n            }\n        }\n    `;\n\n    /** The value (as `<input>.value`). */\n    @property() value = '';\n\n    /** The field's accessible name (Carbon `labelText`; there is no visible label). */\n    @property() label = '';\n\n    @property() placeholder = '';\n\n    @property({ reflect: true }) size: EditInPlaceSize = 'sm';\n\n    @property({ type: Boolean, reflect: true }) invalid = false;\n\n    @property({ attribute: 'invalid-text' }) invalidText = '';\n\n    @property({ type: Boolean, reflect: true }) readonly = false;\n\n    /** Show \"Edit\" without hovering. */\n    @property({ type: Boolean, reflect: true, attribute: 'edit-always-visible' }) editAlwaysVisible = false;\n\n    /** The field takes the surrounding type (a heading made editable). */\n    @property({ type: Boolean, reflect: true, attribute: 'inherit-typography' }) inheritTypography = false;\n\n    /** Where the buttons' tooltips go (Carbon `tooltipAlignment`). */\n    @property({ attribute: 'tooltip-placement' }) tooltipPlacement: Placement = 'top';\n\n    /** Where the read-only note goes (Carbon `toggleTipAlignment`). */\n    @property({ attribute: 'note-placement' }) notePlacement: Placement = 'bottom';\n\n    /** The buttons' names and the read-only note (defaults: the translation's \"Edit\", \"Save\", \"Cancel\", \"Edit off\", …). */\n    @property({ attribute: 'edit-label' }) editLabel = '';\n    @property({ attribute: 'save-label' }) saveLabel = '';\n    @property({ attribute: 'cancel-label' }) cancelLabel = '';\n    @property({ attribute: 'readonly-label' }) readonlyLabel = '';\n    @property({ attribute: 'readonly-text' }) readonlyText = '';\n\n    @state() private editing = false;\n    @state() private noteOpen = false;\n    /** The value when editing started or was last saved. */\n    @state() private initial = '';\n\n    @query('input') private readonly input!: HTMLInputElement;\n\n    private readonly localize = new LocalizeController(this);\n    private readonly ids = uid('ui-edit-in-place');\n\n    override connectedCallback() {\n        super.connectedCallback();\n        this.initial = this.value;\n    }\n\n    protected override willUpdate(changed: Map<string, unknown>) {\n        // a value set from outside while not editing is the new starting point\n        if (changed.has('value') && !this.editing) this.initial = this.value;\n    }\n\n    protected override updated() {\n        this.toggleAttribute('data-editing', this.editing);\n    }\n\n    override focus(options?: FocusOptions) {\n        this.input?.focus(options);\n    }\n\n    private get state() {\n        return editState(this.value, this.initial, this.invalid);\n    }\n\n    private emit(type: 'ui-save' | 'ui-cancel') {\n        this.dispatchEvent(new CustomEvent(type, { detail: { value: this.value }, bubbles: true, composed: true }));\n    }\n\n    private save() {\n        this.initial = this.value;\n        this.emit('ui-save');\n    }\n\n    private cancel() {\n        this.value = this.initial;\n        this.emit('ui-cancel');\n    }\n\n    /** Back in the field, the caret at the end. */\n    private refocus() {\n        const input = this.input;\n        input.focus();\n        input.setSelectionRange(input.value.length, input.value.length);\n    }\n\n    private readonly onFocusIn = () => {\n        if (!this.readonly) this.editing = true;\n    };\n\n    /** Focus leaving the whole control (not to one of its buttons): save a valid change, else put the value back. */\n    private readonly onFocusOut = (event: FocusEvent) => {\n        const next = event.relatedTarget as Node | null;\n        if (next && (this.shadowRoot!.contains(next) || this.contains(next))) return;\n        if (!this.editing) return;\n        this.editing = false;\n        if (leaveAction(this.state.canSave) === 'save') this.save();\n        else this.cancel();\n    };\n\n    /** The field's own `input` (composed) goes on to the host, after this listener (the value already updated). */\n    private readonly onInput = () => {\n        this.value = this.input.value;\n        this.editing = !this.readonly;\n    };\n\n    private readonly onKeydown = (event: KeyboardEvent) => {\n        const action = editKeyAction(event.key, this.state.canSave);\n        if (!action || this.readonly) return;\n        event.preventDefault();\n        if (action === 'save') this.save();\n        else if (action === 'cancel') {\n            this.cancel();\n            this.updateComplete.then(() => this.refocus());\n        } else this.editing = false; // nothing to save: stop editing, the focus stays\n    };\n\n    /** A click in the field (or \"Edit\") starts editing it; a read-only one shows why it can't be. */\n    private readonly onFieldClick = () => {\n        if (this.readonly) this.noteOpen = true;\n        else this.editing = true;\n    };\n\n    /** A press on a button keeps the focus in the field, so leaving isn't triggered on the way. */\n    private readonly keepFocus = (event: MouseEvent) => {\n        if (!this.readonly) event.preventDefault();\n    };\n\n    private readonly onCancelClick = () => {\n        this.cancel();\n        this.updateComplete.then(() => this.refocus());\n    };\n\n    private readonly onSaveClick = () => {\n        if (!this.state.canSave) return;\n        this.save();\n        this.refocus();\n    };\n\n    private renderActions() {\n        const t = this.localize.t.common;\n        if (this.readonly) {\n            return html`<ui-popover high-contrast auto-close placement=${this.notePlacement} ?open=${this.noteOpen} @ui-toggle=${(e: CustomEvent<{ open: boolean }>) => { if (isOwnEvent(e)) this.noteOpen = e.detail.open; }}>\n                <ui-icon-button slot=\"trigger\" kind=\"ghost\" size=\"lg\" label=${this.readonlyLabel || t.editOff} tooltip-placement=${this.tooltipPlacement} haspopup=\"dialog\" .expanded=${this.noteOpen} @click=${() => (this.noteOpen = !this.noteOpen)}>${editOffIcon}</ui-icon-button>\n                <p class=\"note\" id=\"${this.ids}-note\">${this.readonlyText || t.readOnlyField}</p>\n            </ui-popover>`;\n        }\n        if (this.editing) {\n            const { canSave, canCancel } = this.state;\n            return html`<ui-icon-button class=\"cancel\" kind=\"ghost\" size=\"lg\" label=${this.cancelLabel || t.cancel} tooltip-placement=${this.tooltipPlacement} ?disabled=${!canCancel} @mousedown=${this.keepFocus} @click=${this.onCancelClick}>${closeIcon}</ui-icon-button>\n                <ui-icon-button class=\"save\" kind=\"ghost\" size=\"lg\" label=${this.saveLabel || t.save} tooltip-placement=${this.tooltipPlacement} ?disabled=${!canSave} @mousedown=${this.keepFocus} @click=${this.onSaveClick}>${checkIcon}</ui-icon-button>`;\n        }\n        return html`<ui-icon-button class=\"edit\" kind=\"ghost\" size=\"lg\" label=${this.editLabel || t.edit} tooltip-placement=${this.tooltipPlacement} @mousedown=${this.keepFocus} @click=${() => this.refocus()}>${editIcon}</ui-icon-button>`;\n    }\n\n    override render() {\n        const showError = this.invalid && !!this.invalidText;\n        const errorId = `${this.ids}-error`;\n        const error = showError ? html`<p class=\"invalid-text\" part=\"invalid-text\" id=${errorId}>${this.invalidText}</p>` : nothing;\n        const classes = ['container', this.editing ? 'editing' : '', this.invalid && !this.editing ? 'invalid' : '', this.invalid ? 'has-error' : '', this.readonly ? 'readonly' : ''].filter(Boolean).join(' ');\n        return html`<div class=${classes} part=\"base\" @focusin=${this.onFocusIn} @focusout=${this.onFocusOut}>\n                <input\n                    part=\"input\"\n                    type=\"text\"\n                    id=\"${this.ids}-input\"\n                    .value=${this.value}\n                    placeholder=${this.placeholder || nothing}\n                    aria-label=${this.label || nothing}\n                    aria-invalid=${this.invalid ? 'true' : 'false'}\n                    aria-describedby=${showError ? errorId : nothing}\n                    ?readonly=${this.readonly}\n                    @input=${this.onInput}\n                    @keydown=${this.onKeydown}\n                    @click=${this.onFieldClick}\n                />\n                <div class=\"actions\" part=\"actions\">${this.invalid ? warningIcon : nothing}${this.renderActions()}</div>\n            </div>\n            ${error}`;\n    }\n}\n\nexport interface UiEditInPlaceEventMap extends HTMLElementEventMap {\n    'ui-save': CustomEvent<{ value: string }>;\n    'ui-cancel': CustomEvent<{ value: string }>;\n}\n\n/** Typed listeners for UiEditInPlace's events (declaration merging with the class). */\nexport interface UiEditInPlace {\n    addEventListener<K extends keyof UiEditInPlaceEventMap>(type: K, listener: (this: UiEditInPlace, event: UiEditInPlaceEventMap[K]) => unknown, options?: boolean | AddEventListenerOptions): void;\n    addEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions): void;\n    removeEventListener<K extends keyof UiEditInPlaceEventMap>(type: K, listener: (this: UiEditInPlace, event: UiEditInPlaceEventMap[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-edit-in-place': UiEditInPlace;\n    }\n}\n"
    }
  ]
}
