{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "tw-edit-in-place",
  "type": "registry:ui",
  "title": "react-tw/edit-in-place.tsx",
  "description": "Carbon for IBM Products `EditInPlace`, as `<ui-edit-in-place>`: a value shown as plain text that is edited right where 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": [
    "@purpurabog/ui@^2.3.0"
  ],
  "registryDependencies": [
    "@kanok-ui/core-edit-in-place",
    "@kanok-ui/core-position",
    "@kanok-ui/tw-i18n",
    "@kanok-ui/tw-icon-button",
    "@kanok-ui/tw-popover",
    "@kanok-ui/tw-theme",
    "@kanok-ui/tw-utils"
  ],
  "files": [
    {
      "path": "src/react-tw/edit-in-place.tsx",
      "type": "registry:ui",
      "target": "components/ui/react-tw/edit-in-place.tsx",
      "content": "import type { ComponentProps, ReactNode } from 'react';\nimport type { EditInPlaceSize } from '../core/edit-in-place.ts';\nimport type { Placement } from '../core/position.ts';\nimport { EditInPlace as EditInPlacePrimitive } from '@purpurabog/ui/headless';\nimport { IconButton } from './icon-button.tsx';\nimport { Popover } from './popover.tsx';\nimport { useTranslation } from './lib/i18n.ts';\nimport { cn } from './lib/utils.ts';\n\nexport type { EditInPlaceSize } from '../core/edit-in-place.ts';\n\n// Carbon icons (16px): Edit, EditOff, Close, Checkmark, WarningFilled\nconst editIcon = (\n    <svg viewBox=\"0 0 32 32\" aria-hidden=\"true\">\n        <path d=\"M2 26H30V28H2z\" />\n        <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\" />\n    </svg>\n);\nconst editOffIcon = (\n    <svg viewBox=\"0 0 32 32\" aria-hidden=\"true\">\n        <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\" />\n        <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\" />\n    </svg>\n);\nconst closeIcon = (\n    <svg viewBox=\"0 0 32 32\" aria-hidden=\"true\">\n        <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\" />\n    </svg>\n);\nconst checkIcon = (\n    <svg viewBox=\"0 0 32 32\" aria-hidden=\"true\">\n        <path d=\"M13 24 4 15l1.414-1.414L13 21.171 26.586 7.586 28 9 13 24z\" />\n    </svg>\n);\nconst warningIcon = (\n    <svg viewBox=\"0 0 16 16\" aria-hidden=\"true\" className=\"mx-2 my-auto size-4 shrink-0 fill-error forced-colors:fill-[CanvasText]\">\n        <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\" />\n    </svg>\n);\n\nconst SIZE: Record<EditInPlaceSize, string> = { sm: '[--_size:40px]', md: '[--_size:48px]', lg: '[--_size:64px]' };\n\n/** A ghost square button of the toolbar, as tall as the control. */\nfunction ActionButton({ children, ...rest }: Readonly<ComponentProps<typeof IconButton>>) {\n    return (\n        <IconButton kind=\"ghost\" size=\"lg\" {...rest}>\n            {children}\n        </IconButton>\n    );\n}\n\ninterface EditActionsProps {\n    editAlwaysVisible: boolean;\n    tooltipPlacement: Placement;\n    notePlacement: Placement;\n    labels: { edit: string; save: string; cancel: string; readOnly: string; note: string };\n}\n\n/** The toolbar's buttons: \"Edit\" at rest (on hover), \"Cancel\" and \"Save\" while editing, \"Edit off\" and its note when read-only. */\nfunction EditActions({ editAlwaysVisible, tooltipPlacement, notePlacement, labels }: Readonly<EditActionsProps>) {\n    const { mode, noteOpen, setNoteOpen } = EditInPlacePrimitive.useEditInPlaceContext();\n    if (mode === 'readonly') {\n        return (\n            <Popover\n                open={noteOpen}\n                onOpenChange={setNoteOpen}\n                autoClose\n                highContrast\n                placement={notePlacement}\n                trigger={\n                    <ActionButton label={labels.readOnly} tooltipPlacement={tooltipPlacement} aria-haspopup=\"dialog\" onClick={() => setNoteOpen(!noteOpen)}>\n                        {editOffIcon}\n                    </ActionButton>\n                }\n            >\n                <p className=\"m-0\">{labels.note}</p>\n            </Popover>\n        );\n    }\n    // one primitive part for all three: \"Edit\" turning into \"Cancel\" keeps its element, as one ActionButton did\n    if (mode === 'editing') {\n        return (\n            <>\n                <EditInPlacePrimitive.Action\n                    action=\"cancel\"\n                    render={(props) => (\n                        <ActionButton {...props} label={labels.cancel} tooltipPlacement={tooltipPlacement}>\n                            {closeIcon}\n                        </ActionButton>\n                    )}\n                />\n                <EditInPlacePrimitive.Action\n                    action=\"save\"\n                    render={(props) => (\n                        <ActionButton {...props} label={labels.save} tooltipPlacement={tooltipPlacement}>\n                            {checkIcon}\n                        </ActionButton>\n                    )}\n                />\n            </>\n        );\n    }\n    return (\n        <EditInPlacePrimitive.Action\n            action=\"edit\"\n            render={(props) => (\n                <ActionButton {...props} label={labels.edit} tooltipPlacement={tooltipPlacement} className={editAlwaysVisible ? undefined : 'invisible group-hover/eip:visible'}>\n                    {editIcon}\n                </ActionButton>\n            )}\n        />\n    );\n}\n\nexport interface EditInPlaceProps extends Omit<ComponentProps<'div'>, 'onChange' | 'defaultValue' | 'children'> {\n    /** Controlled value (pair with `onChange`). */\n    value?: string;\n    defaultValue?: string;\n    /** Every keystroke, and the value put back on cancel. */\n    onChange?: (value: string) => void;\n    /** A change was saved (Save, Enter, leaving it). */\n    onSave?: (value: string) => void;\n    /** The value was put back (Cancel, Escape, leaving it without a valid change). */\n    onCancel?: (value: string) => void;\n    /** The field's accessible name (Carbon `labelText`). */\n    label?: string;\n    placeholder?: string;\n    size?: EditInPlaceSize;\n    invalid?: boolean;\n    invalidText?: ReactNode;\n    readOnly?: boolean;\n    editAlwaysVisible?: boolean;\n    inheritTypography?: boolean;\n    tooltipPlacement?: Placement;\n    notePlacement?: Placement;\n    editLabel?: string;\n    saveLabel?: string;\n    cancelLabel?: string;\n    readOnlyLabel?: string;\n    readOnlyText?: string;\n    lang?: string;\n}\n\n/**\n * Carbon for IBM Products `EditInPlace`, as `<ui-edit-in-place>`: a value shown as plain text that is edited right where\n * it is — hovered, the field fill and \"Edit\"; focused, \"Cancel\" and \"Save\" (enabled once the value changed). Enter saves\n * and keeps editing (with nothing to save it stops editing, the focus staying); Escape puts the value back; leaving it\n * saves a valid change and otherwise puts the value back. Sizes sm 40 / md 48 / lg 64. The behaviour is the headless\n * EditInPlace.\n */\nexport function EditInPlace({ value, defaultValue = '', onChange, onSave, onCancel, invalid = false, readOnly = false, ...rest }: Readonly<EditInPlaceProps>) {\n    return (\n        <EditInPlacePrimitive.Root value={value} defaultValue={defaultValue} onValueChange={onChange} onSave={onSave} onCancel={onCancel} invalid={invalid} readOnly={readOnly}>\n            <EditInPlaceField {...rest} />\n        </EditInPlacePrimitive.Root>\n    );\n}\n\n/** The box, field, buttons and error, inside the primitive (they follow its editing state). */\nfunction EditInPlaceField({\n    label,\n    placeholder,\n    size = 'sm',\n    invalidText,\n    editAlwaysVisible = false,\n    inheritTypography = false,\n    tooltipPlacement = 'top',\n    notePlacement = 'bottom',\n    editLabel,\n    saveLabel,\n    cancelLabel,\n    readOnlyLabel,\n    readOnlyText,\n    lang,\n    className,\n    ...rest\n}: Readonly<Omit<EditInPlaceProps, 'value' | 'defaultValue' | 'onChange' | 'onSave' | 'onCancel' | 'invalid' | 'readOnly'>>) {\n    const t = useTranslation(lang).common;\n    const { editing, invalid, readOnly } = EditInPlacePrimitive.useEditInPlaceContext();\n    return (\n        <div {...rest} className={cn('block', className)}>\n            <EditInPlacePrimitive.Group\n                className={cn(\n                    'group/eip box-border flex min-h-(--_size) cursor-pointer items-center bg-transparent hover:bg-field-1',\n                    SIZE[size],\n                    // Carbon: field-01 while editing; the focus line around it while the field has focus\n                    editing && 'bg-field-1 has-[input:focus]:rounded-[var(--ui-field-radius,0px)] has-[input:focus]:outline-[length:var(--ui-focus-width,2px)] has-[input:focus]:outline-offset-0 has-[input:focus]:outline-solid has-[input:focus]:outline-focus forced-colors:has-[input:focus]:outline-[Highlight]',\n                    invalid && !editing && 'border-2 border-solid border-error',\n                )}\n            >\n                <EditInPlacePrimitive.Input\n                    placeholder={placeholder}\n                    aria-label={label}\n                    className={cn(\n                        'm-0 box-border h-10 min-w-0 flex-1 cursor-pointer border-0 bg-transparent px-4 py-0 text-ink type-body-sm leading-[18px]! outline-none placeholder:text-ink-placeholder placeholder:opacity-100',\n                        inheritTypography ? '[font-size:inherit]! [font-weight:inherit]! [letter-spacing:inherit]! [line-height:inherit]!' : '[font-size:var(--ui-field-font-size,14px)]!',\n                    )}\n                />\n                {/* the buttons: squares of the height; the box opens to two of them while editing (Carbon slide-in, 150ms) */}\n                <div\n                    className={cn(\n                        'flex shrink-0 items-center justify-end self-stretch [--ui-field-height-lg:var(--_size)] [&_svg]:size-4 [&_svg]:fill-current',\n                        invalid ? '[--_editing-width:calc(var(--_size)*2+32px)] [--_width:calc(var(--_size)+32px)]' : '[--_editing-width:calc(var(--_size)*2)] [--_width:var(--_size)]',\n                        editing && !readOnly ? 'w-(--_editing-width) animate-ui-open-actions motion-reduce:animate-none' : 'w-(--_width)',\n                    )}\n                >\n                    {invalid ? warningIcon : null}\n                    <EditActions\n                        editAlwaysVisible={editAlwaysVisible}\n                        tooltipPlacement={tooltipPlacement}\n                        notePlacement={notePlacement}\n                        labels={{ edit: editLabel || t.edit, save: saveLabel || t.save, cancel: cancelLabel || t.cancel, readOnly: readOnlyLabel || t.editOff, note: readOnlyText || t.readOnlyField }}\n                    />\n                </div>\n            </EditInPlacePrimitive.Group>\n            {/* shown while invalid, describing the field */}\n            {invalidText ? <EditInPlacePrimitive.InvalidText className=\"mt-2 mb-0 text-error-text type-caption leading-4!\">{invalidText}</EditInPlacePrimitive.InvalidText> : null}\n        </div>\n    );\n}\n"
    }
  ]
}
