{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "feedback-web",
  "title": "Feedback (Web Components)",
  "description": "Thumbs up and down for a response, with an optional comment written in a popover.",
  "dependencies": [
    "@kind-ui/agent-ui@0.9.0",
    "@ui-sdk/core@0.1.0-beta.17",
    "@ui-sdk/web@0.1.0-beta.17"
  ],
  "registryDependencies": [
    "@kind-ui/agent-ui-setup",
    "@kind-ui/internal-textarea-web"
  ],
  "files": [
    {
      "path": "packages/agent-ui/src/web/feedback.ts",
      "content": "import {\n  type FeedbackApi,\n  type FeedbackRating,\n  type FeedbackRatingChangeReason,\n  type FeedbackState,\n  feedbackPrimitive,\n} from \"@kind-ui/agent-ui/core\";\nimport type { ActionDetails } from \"@ui-sdk/core\";\nimport type { PopoverOpenChangeReason } from \"@ui-sdk/core/popover\";\nimport { createParts, type UiActionDetail, wrappedButton, wrappedChild } from \"@ui-sdk/web\";\nimport type { AttributeProperties, UiChangeEvent, UiElement } from \"@ui-sdk/web/events\";\nimport \"@ui-sdk/web/toggle\";\nimport { defineInternalTextarea } from \"./internal/textarea\";\n\nconst THUMB = (value: \"up\" | \"down\", label: string) =>\n  `<ui-toggle value=\"${value}\"><button aria-label=\"${label}\" data-kind-icon-button data-kind-size=\"sm\"><ui-toggle-icon data-feedback-icon=\"${value}\"></ui-toggle-icon></button></ui-toggle>`;\n\nconst DEFAULT_PARTS = `<kind-feedback-thumbs><ui-toggle-group data-agent-hidden>${THUMB(\"up\", \"Good response\")}${THUMB(\"down\", \"Bad response\")}</ui-toggle-group></kind-feedback-thumbs><kind-feedback-comment><button>Add a comment</button></kind-feedback-comment><kind-feedback-thanks>Thanks for your feedback</kind-feedback-thanks><kind-feedback-content hidden><kind-feedback-title><h2>Add a comment</h2></kind-feedback-title><kind-feedback-comment-text><kind-internal-textarea><ui-textarea><textarea rows=\"3\"></textarea></ui-textarea></kind-internal-textarea></kind-feedback-comment-text><div data-feedback-actions><kind-feedback-comment-status><kind-feedback-comment-status-icon></kind-feedback-comment-status-icon>Sending</kind-feedback-comment-status><kind-feedback-comment-error>Comment not sent. Try again.</kind-feedback-comment-error><kind-feedback-comment-send><button>Send</button></kind-feedback-comment-send></div></kind-feedback-content>`;\n\n// The text box this element filled in. Kept across instances, since UI SDK rebuilds one when the element returns.\nconst owned = new WeakSet<Element>();\n\nconst write = <Target extends object, Key extends keyof Target>(\n  target: Target | null,\n  key: Key,\n  value: Target[Key],\n) => {\n  if (target && target[key] !== value) target[key] = value;\n};\n\n/**\n * `<kind-feedback>` fills in its parts when it has no children. Pressing a thumb fires `ratingchange` with the rating\n * as `detail.value`. The `can-comment` attribute shows \"Add a comment\" once there's a rating, whose Send fires\n * `comment` with the text as `detail.value`: call `event.detail.waitUntil(promise)` to keep Send disabled until it\n * settles.\n */\nexport function defineFeedback(): void {\n  if (customElements.get(\"kind-feedback\")) return;\n  defineInternalTextarea();\n  createParts(feedbackPrimitive, {\n    root: {\n      tag: \"kind-feedback\",\n      kinds: { canComment: \"boolean\" },\n      // Without the attribute the callback is left out; with it, `@ui-sdk/web` fills it in with an event.\n      options: (el) => ((el as KindFeedbackElement).canComment ? {} : { onComment: undefined }),\n      state: {\n        rating: {\n          get: (api) => api.getRating(),\n          set: (api, rating) => api.rate(rating as FeedbackRating),\n        },\n        open: {\n          get: (api) => api.isOpen(),\n          set: (api, open) => api.setOpen(!!open),\n        },\n      },\n      api: [\"getRating\", \"rate\", \"isOpen\", \"setOpen\"],\n      render: (el, instance) => {\n        if (!el.firstElementChild) {\n          el.innerHTML = DEFAULT_PARTS;\n          const filled = el.querySelector(\"kind-feedback-comment-text textarea\");\n          if (filled) owned.add(filled);\n        }\n        const api = instance.api as FeedbackApi;\n        const draw = () => {\n          const state = instance.get() as FeedbackState;\n          const { controller, \"aria-label\": ariaLabel } = api.thumbsState();\n          const group = el.querySelector<Element & { controller?: unknown }>(\n            \"kind-feedback-thumbs ui-toggle-group\",\n          );\n          write(group, \"controller\", controller);\n          write(group, \"ariaLabel\", ariaLabel);\n          api.syncRating();\n          const text = api.commentTextState(state);\n          write(\n            el.querySelector<Element & { controller?: unknown }>(\"kind-feedback-comment-text ui-textarea\"),\n            \"controller\",\n            text.controller,\n          );\n          const textarea = el.querySelector<HTMLTextAreaElement>(\"kind-feedback-comment-text textarea\");\n          write(textarea, \"readOnly\", text.readOnly);\n          // A text box someone wrote keeps its own words.\n          if (textarea && owned.has(textarea)) {\n            write(textarea, \"placeholder\", text.placeholder);\n            write(textarea, \"ariaLabel\", text[\"aria-label\"]);\n          }\n        };\n        draw();\n        return instance.subscribe(draw);\n      },\n    },\n    thumbs: { tag: \"kind-feedback-thumbs\", display: \"contents\" },\n    comment: { tag: \"kind-feedback-comment\", target: wrappedButton, display: \"contents\" },\n    thanks: \"kind-feedback-thanks\",\n    content: \"kind-feedback-content\",\n    title: { tag: \"kind-feedback-title\", target: wrappedChild(\"h2\"), display: \"contents\" },\n    \"comment-text\": \"kind-feedback-comment-text\",\n    \"comment-send\": { tag: \"kind-feedback-comment-send\", target: wrappedButton, display: \"contents\" },\n    \"comment-status\": \"kind-feedback-comment-status\",\n    \"comment-status-icon\": \"kind-feedback-comment-status-icon\",\n    \"comment-error\": \"kind-feedback-comment-error\",\n  });\n}\n\nexport type KindFeedbackEvents = {\n  ratingchange: UiChangeEvent<FeedbackRating, FeedbackRatingChangeReason>;\n  comment: CustomEvent<{ value: string } & ActionDetails & Pick<UiActionDetail, \"waitUntil\">>;\n  openchange: UiChangeEvent<boolean, PopoverOpenChangeReason>;\n};\n\nexport interface KindFeedbackElement\n  extends UiElement<KindFeedbackEvents>,\n    AttributeProperties<typeof feedbackPrimitive, \"api\" | \"rating\" | \"defaultRating\" | \"open\"> {\n  canComment: boolean;\n  rating: FeedbackRating;\n  defaultRating?: FeedbackRating;\n  open: boolean;\n  api: Pick<FeedbackApi, \"getRating\" | \"rate\" | \"isOpen\" | \"setOpen\">;\n}\n\ndeclare global {\n  interface HTMLElementTagNameMap {\n    \"kind-feedback\": KindFeedbackElement;\n    \"kind-feedback-thumbs\": HTMLElement;\n    \"kind-feedback-comment\": HTMLElement;\n    \"kind-feedback-thanks\": HTMLElement;\n    \"kind-feedback-content\": HTMLElement;\n    \"kind-feedback-title\": HTMLElement;\n    \"kind-feedback-comment-text\": HTMLElement;\n    \"kind-feedback-comment-send\": HTMLElement;\n    \"kind-feedback-comment-status\": HTMLElement;\n    \"kind-feedback-comment-status-icon\": HTMLElement;\n    \"kind-feedback-comment-error\": HTMLElement;\n  }\n}\n",
      "type": "registry:ui",
      "target": "@ui/web/feedback.ts"
    }
  ],
  "meta": {
    "layer": "web"
  },
  "type": "registry:ui"
}
