@designbasekorea/ui-wc 0.5.3 → 0.5.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (156) hide show
  1. package/dist/chunks/lottie-e273b936.js +17032 -0
  2. package/dist/esm/components/animation-background/canvas-controller.js +121 -1
  3. package/dist/esm/components/animation-background/controls.js +180 -1
  4. package/dist/esm/components/animation-background/graphite-controller.js +196 -1
  5. package/dist/esm/components/animation-background/graphite-presets.js +42 -1
  6. package/dist/esm/components/animation-background/graphite-shader.js +111 -1
  7. package/dist/esm/components/animation-background/layers.js +369 -1
  8. package/dist/esm/components/animation-background/lumina-presets.js +53 -1
  9. package/dist/esm/components/animation-background/lumina-shaders.js +181 -1
  10. package/dist/esm/components/animation-background/lumina-utils.js +60 -1
  11. package/dist/esm/components/animation-background/particle-defaults.js +36 -1
  12. package/dist/esm/components/animation-background/renderers.js +167 -1
  13. package/dist/esm/components/animation-background/shader-controller.js +154 -1
  14. package/dist/esm/components/color-picker-utils.js +134 -1
  15. package/dist/esm/components/date-picker-utils.js +140 -1
  16. package/dist/esm/components/db-accordion.js +304 -1
  17. package/dist/esm/components/db-ad-banner.js +455 -1
  18. package/dist/esm/components/db-alert.js +149 -1
  19. package/dist/esm/components/db-animation-background.js +486 -1
  20. package/dist/esm/components/db-animation-text.js +601 -1
  21. package/dist/esm/components/db-app-badge.js +88 -1
  22. package/dist/esm/components/db-audio-player.js +752 -1
  23. package/dist/esm/components/db-avatar.js +188 -1
  24. package/dist/esm/components/db-backdrop.js +139 -1
  25. package/dist/esm/components/db-badge.js +91 -1
  26. package/dist/esm/components/db-banner.js +188 -1
  27. package/dist/esm/components/db-bottom-navigation.js +146 -1
  28. package/dist/esm/components/db-bottom-sheet.js +481 -1
  29. package/dist/esm/components/db-breadcrumbs.js +329 -1
  30. package/dist/esm/components/db-button.js +316 -1
  31. package/dist/esm/components/db-calendar.js +675 -1
  32. package/dist/esm/components/db-card.js +265 -1
  33. package/dist/esm/components/db-carousel.js +264 -1
  34. package/dist/esm/components/db-checkbox.js +202 -1
  35. package/dist/esm/components/db-chip.js +104 -1
  36. package/dist/esm/components/db-code-block.js +304 -1
  37. package/dist/esm/components/db-color-picker.js +746 -1
  38. package/dist/esm/components/db-confirm.js +191 -1
  39. package/dist/esm/components/db-container.js +39 -1
  40. package/dist/esm/components/db-context-menu.js +265 -1
  41. package/dist/esm/components/db-countdown.js +146 -1
  42. package/dist/esm/components/db-cursor-follower.js +173 -1
  43. package/dist/esm/components/db-date-picker.js +837 -1
  44. package/dist/esm/components/db-divider.js +66 -1
  45. package/dist/esm/components/db-drawer.js +245 -1
  46. package/dist/esm/components/db-dropdown.js +493 -1
  47. package/dist/esm/components/db-dropzone.js +266 -1
  48. package/dist/esm/components/db-empty-state.js +97 -1
  49. package/dist/esm/components/db-file-uploader.js +256 -1
  50. package/dist/esm/components/db-floating-action-button.js +139 -1
  51. package/dist/esm/components/db-footer.js +385 -1
  52. package/dist/esm/components/db-form.js +566 -1
  53. package/dist/esm/components/db-gantt.js +882 -1
  54. package/dist/esm/components/db-gradient.js +133 -1
  55. package/dist/esm/components/db-grid-background.js +909 -1
  56. package/dist/esm/components/db-grid.js +73 -1
  57. package/dist/esm/components/db-icons.js +82 -1
  58. package/dist/esm/components/db-image-list.js +214 -1
  59. package/dist/esm/components/db-image-placeholder.js +96 -1
  60. package/dist/esm/components/db-image.js +289 -1
  61. package/dist/esm/components/db-indicator.js +517 -1
  62. package/dist/esm/components/db-input.js +420 -1
  63. package/dist/esm/components/db-kanban.js +538 -1
  64. package/dist/esm/components/db-label.js +66 -1
  65. package/dist/esm/components/db-lightbox.js +372 -1
  66. package/dist/esm/components/db-list.js +340 -1
  67. package/dist/esm/components/db-logo-svg.js +72 -1
  68. package/dist/esm/components/db-logo.js +106 -1
  69. package/dist/esm/components/db-lottie.js +109 -1
  70. package/dist/esm/components/db-markdown-editor.js +458 -1
  71. package/dist/esm/components/db-marquee.js +402 -1
  72. package/dist/esm/components/db-masonry.js +94 -1
  73. package/dist/esm/components/db-menu-item.js +211 -1
  74. package/dist/esm/components/db-modal.js +522 -1
  75. package/dist/esm/components/db-navbar.js +680 -1
  76. package/dist/esm/components/db-onboarding-modal.js +322 -1
  77. package/dist/esm/components/db-page-header.js +312 -1
  78. package/dist/esm/components/db-pagination.js +271 -1
  79. package/dist/esm/components/db-popover.js +580 -1
  80. package/dist/esm/components/db-progress-step.js +166 -1
  81. package/dist/esm/components/db-progress.js +171 -1
  82. package/dist/esm/components/db-progressbar.js +72 -1
  83. package/dist/esm/components/db-radio.js +172 -1
  84. package/dist/esm/components/db-random-gradient.js +121 -1
  85. package/dist/esm/components/db-range-slider.js +811 -1
  86. package/dist/esm/components/db-rating.js +325 -1
  87. package/dist/esm/components/db-reorder.js +286 -1
  88. package/dist/esm/components/db-resizable-panels.js +282 -1
  89. package/dist/esm/components/db-scroll-area.js +126 -1
  90. package/dist/esm/components/db-search-bar.js +668 -1
  91. package/dist/esm/components/db-section-app-download.js +101 -1
  92. package/dist/esm/components/db-section-contact.js +102 -1
  93. package/dist/esm/components/db-section-cta.js +150 -1
  94. package/dist/esm/components/db-section-faq.js +120 -1
  95. package/dist/esm/components/db-section-feature-grid.js +137 -1
  96. package/dist/esm/components/db-section-feature.js +179 -1
  97. package/dist/esm/components/db-section-hero.js +243 -1
  98. package/dist/esm/components/db-section-logo-cloud.js +138 -1
  99. package/dist/esm/components/db-section-pricing.js +166 -1
  100. package/dist/esm/components/db-section-stats.js +129 -1
  101. package/dist/esm/components/db-section-testimonials.js +252 -1
  102. package/dist/esm/components/db-section.js +385 -1
  103. package/dist/esm/components/db-segment-control.js +231 -1
  104. package/dist/esm/components/db-select.js +571 -1
  105. package/dist/esm/components/db-share-icons.js +40 -1
  106. package/dist/esm/components/db-share.js +489 -1
  107. package/dist/esm/components/db-sidebar.js +1068 -1
  108. package/dist/esm/components/db-skeleton.js +94 -1
  109. package/dist/esm/components/db-spinner.js +102 -1
  110. package/dist/esm/components/db-split-view.js +297 -1
  111. package/dist/esm/components/db-stack.js +46 -1
  112. package/dist/esm/components/db-stat.js +218 -1
  113. package/dist/esm/components/db-stepper.js +398 -1
  114. package/dist/esm/components/db-table.js +412 -1
  115. package/dist/esm/components/db-tabs.js +335 -1
  116. package/dist/esm/components/db-testimonial.js +162 -1
  117. package/dist/esm/components/db-textarea.js +233 -1
  118. package/dist/esm/components/db-time-picker.js +523 -1
  119. package/dist/esm/components/db-timeline.js +174 -1
  120. package/dist/esm/components/db-toast.js +158 -1
  121. package/dist/esm/components/db-toggle.js +162 -1
  122. package/dist/esm/components/db-toolbar.js +94 -1
  123. package/dist/esm/components/db-tooltip.js +440 -1
  124. package/dist/esm/components/db-top-banner.js +121 -1
  125. package/dist/esm/components/db-tutorial.js +324 -1
  126. package/dist/esm/components/db-video-player.js +503 -1
  127. package/dist/esm/components/db-youtube-player.js +194 -1
  128. package/dist/esm/components/highlight-code.js +65 -1
  129. package/dist/esm/components/input-validation.js +50 -1
  130. package/dist/esm/components/marketing-block-host-utils.js +9 -1
  131. package/dist/esm/components/number-scrub.js +117 -1
  132. package/dist/esm/components/random-gradient-palettes.js +67 -1
  133. package/dist/esm/components/section-block-header-utils.js +48 -1
  134. package/dist/esm/helpers/confirm.js +52 -1
  135. package/dist/esm/helpers/toast.js +73 -1
  136. package/dist/esm/index.js +111 -1
  137. package/dist/esm/node_modules/prismjs/components/prism-bash.js +235 -1
  138. package/dist/esm/node_modules/prismjs/components/prism-clike.js +31 -1
  139. package/dist/esm/node_modules/prismjs/components/prism-css.js +64 -1
  140. package/dist/esm/node_modules/prismjs/components/prism-javascript.js +172 -1
  141. package/dist/esm/node_modules/prismjs/components/prism-json.js +27 -1
  142. package/dist/esm/node_modules/prismjs/components/prism-jsx.js +143 -1
  143. package/dist/esm/node_modules/prismjs/components/prism-markup.js +186 -1
  144. package/dist/esm/node_modules/prismjs/components/prism-scss.js +81 -1
  145. package/dist/esm/node_modules/prismjs/components/prism-tsx.js +15 -1
  146. package/dist/esm/node_modules/prismjs/components/prism-typescript.js +60 -1
  147. package/dist/index.cjs +55414 -1
  148. package/dist/index.css +1 -1
  149. package/dist/index.esm.js +38284 -1
  150. package/dist/react/chunks/lottie-72a4c6d4.js +17032 -0
  151. package/dist/react/index.cjs +56503 -1
  152. package/dist/react/index.d.ts +1 -1
  153. package/dist/react/index.esm.js +39373 -1
  154. package/package.json +1 -1
  155. package/dist/chunks/lottie-2d1f6278.js +0 -1
  156. package/dist/react/chunks/lottie-14e7addb.js +0 -1
@@ -1 +1,668 @@
1
- import{getDbComponentIconSize as e,createDbIcon as t}from"./db-icons.js";const s=["s","m","l"],a=["vertical","horizontal"];class DbSearchBar extends HTMLElement{constructor(){super(...arguments),this.recentSearches=[],this.isFocused=!1,this.currentSuggestion=0,this.scrollParents=[]}static get observedAttributes(){return["value","default-value","placeholder","size","disabled","readonly","full-width","show-shortcut","shortcut-label","enable-recent-searches","recent-searches-key","recent-searches-layout","suggested-searches","suggestion-rolling-interval","categories","selected-category","search-results","empty-state-message"]}getResolvedSize(){const e=this.getAttribute("size")||"m";return s.includes(e)?e:"m"}getIconPx(){return e(this.getResolvedSize())}syncIcons(){const e=this.getIconPx();this.querySelector(".designbase-wc-search-bar__search-icon")?.replaceChildren(t("search",e)),this.clearButton?.replaceChildren(t("close",e))}hasInputValue(){return Boolean((this.input?.value??this.getAttribute("value")??"").trim())}get value(){return this.input?.value??this.getAttribute("value")??""}set value(e){this.setAttribute("value",e),this.input&&(this.input.value=e)}connectedCallback(){this.loadRecentSearches(),this.render(),this.bindGlobalShortcut()}disconnectedCallback(){this.clearSuggestionInterval(),this.unbindGlobalShortcut(),this.detachDropdownPortal(),this.blurTimer&&clearTimeout(this.blurTimer)}attributeChangedCallback(e){if(this.isConnected)return"value"===e&&this.input?(this.input.value=this.getAttribute("value")||"",this.syncValueState(),this.syncPlaceholder(),this.updateDropdown(),void this.syncSuggestionRolling()):"disabled"===e||"readonly"===e||"placeholder"===e||"show-shortcut"===e||"shortcut-label"===e||"selected-category"===e||"search-results"===e||"empty-state-message"===e||"suggested-searches"===e?(this.syncAttributes(),this.syncPlaceholder(),this.updateDropdown(),void this.syncSuggestionRolling()):"enable-recent-searches"===e||"recent-searches-key"===e?(this.loadRecentSearches(),void this.updateDropdown()):void("categories"!==e&&"recent-searches-layout"!==e?this.render():this.updateDropdown())}focus(){this.input?.focus()}parseJsonArray(e){try{const t=JSON.parse(this.getAttribute(e)||"[]");return Array.isArray(t)?t:[]}catch{return[]}}getRecentSearchesKey(){return this.getAttribute("recent-searches-key")||"searchbar-recent-searches"}loadRecentSearches(){if(this.hasAttribute("enable-recent-searches"))try{const e=localStorage.getItem(this.getRecentSearchesKey());this.recentSearches=e?JSON.parse(e):[],Array.isArray(this.recentSearches)||(this.recentSearches=[])}catch{this.recentSearches=[]}else this.recentSearches=[]}saveRecentSearches(){try{localStorage.setItem(this.getRecentSearchesKey(),JSON.stringify(this.recentSearches))}catch{}}clearSuggestionInterval(){this.suggestionInterval&&(clearInterval(this.suggestionInterval),this.suggestionInterval=void 0)}syncSuggestionRolling(){this.clearSuggestionInterval();const e=this.parseJsonArray("suggested-searches"),t=this.hasInputValue(),s=Number(this.getAttribute("suggestion-rolling-interval")||5e3);e.length>0&&!t&&(this.suggestionInterval=setInterval(()=>{this.currentSuggestion=(this.currentSuggestion+1)%e.length,this.syncPlaceholder(),this.updateDropdown()},s))}syncPlaceholder(){if(!this.input)return;const e=this.parseJsonArray("suggested-searches"),t=this.hasInputValue(),s=this.getAttribute("placeholder")||"검색...";e.length>0&&!t?this.input.placeholder=e[this.currentSuggestion]||s:this.input.placeholder=s}syncValueState(){const e=this.hasInputValue();this.classList.toggle("designbase-wc-search-bar--has-value",e),this.clearButton&&(this.clearButton.hidden=!e||this.hasAttribute("disabled")||this.hasAttribute("readonly")),this.shortcutBadge&&(this.shortcutBadge.hidden=!this.hasAttribute("show-shortcut")||e||this.hasAttribute("disabled")||this.hasAttribute("readonly")),this.syncIcons()}syncAttributes(){const e=this.getAttribute("size")||"m",t=this.hasAttribute("disabled"),a=this.hasAttribute("readonly"),n=this.hasAttribute("show-shortcut");this.className=["designbase-wc-search-bar",`designbase-wc-search-bar--${s.includes(e)?e:"m"}`,t&&"designbase-wc-search-bar--disabled",a&&"designbase-wc-search-bar--readonly",this.hasAttribute("full-width")&&"designbase-wc-search-bar--full-width",Boolean(this.hasInputValue())&&"designbase-wc-search-bar--has-value"].filter(Boolean).join(" "),this.input&&(this.input.disabled=t,this.input.readOnly=a),this.shortcutBadge&&(this.shortcutBadge.hidden=!n||this.hasInputValue()||t||a,this.shortcutBadge.textContent=this.getAttribute("shortcut-label")||"⌘K"),this.syncValueState()}bindGlobalShortcut(){this.unbindGlobalShortcut(),!this.hasAttribute("show-shortcut")||this.hasAttribute("disabled")||this.hasAttribute("readonly")||(this.globalKeyHandler=e=>{const t=e.metaKey||e.ctrlKey;let s="k";const a=(this.getAttribute("shortcut-label")||"⌘K").slice(-1).toLowerCase();a&&(s=a),t&&e.key.toLowerCase()===s&&(e.preventDefault(),this.focus())},window.addEventListener("keydown",this.globalKeyHandler))}unbindGlobalShortcut(){this.globalKeyHandler&&(window.removeEventListener("keydown",this.globalKeyHandler),this.globalKeyHandler=void 0)}positionDropdown(){if(!this.dropdown||this.dropdown.hidden)return;const e=this.getBoundingClientRect(),t=this.input?.getBoundingClientRect(),s=t??e;this.dropdown.style.top=`${s.bottom+4}px`,this.dropdown.style.left=`${e.left}px`,this.dropdown.style.width=`${e.width}px`,this.dropdown.style.right="auto"}bindRepositionListeners(){if(this.repositionHandler)return;this.repositionHandler=()=>{requestAnimationFrame(()=>this.positionDropdown())},window.addEventListener("scroll",this.repositionHandler,!0),window.addEventListener("resize",this.repositionHandler),this.scrollParents=[];let e=this.parentElement;for(;e&&e!==document.body;){const t=getComputedStyle(e),s=/(auto|scroll|overlay)/.test(t.overflowY),a=/(auto|scroll|overlay)/.test(t.overflow);(s||a)&&e.scrollHeight>e.clientHeight&&(e.addEventListener("scroll",this.repositionHandler,{passive:!0}),this.scrollParents.push(e)),e=e.parentElement}}unbindRepositionListeners(){this.repositionHandler&&(window.removeEventListener("scroll",this.repositionHandler,!0),window.removeEventListener("resize",this.repositionHandler),this.scrollParents.forEach(e=>e.removeEventListener("scroll",this.repositionHandler)),this.scrollParents=[],this.repositionHandler=void 0)}attachDropdownPortal(){this.dropdown&&!this.dropdown.hidden&&(this.dropdown.classList.add("designbase-wc-search-bar__dropdown--portal"),this.dropdown.parentElement!==document.body&&document.body.append(this.dropdown),requestAnimationFrame(()=>this.positionDropdown()),this.repositionHandler||this.bindRepositionListeners())}detachDropdownPortal(){this.unbindRepositionListeners(),this.dropdown&&(this.dropdown.classList.remove("designbase-wc-search-bar__dropdown--portal"),this.dropdown.style.top="",this.dropdown.style.left="",this.dropdown.style.width="",this.dropdown.style.right="",this.dropdown.parentElement===document.body&&this.isConnected&&this.append(this.dropdown))}shouldShowDropdown(){const e=this.hasInputValue(),t=this.parseJsonArray("categories"),s=this.parseJsonArray("suggested-searches"),a=this.getAttribute("search-results"),n=this.getAttribute("empty-state-message");return this.isFocused&&(t.length>0||this.hasAttribute("enable-recent-searches")&&this.recentSearches.length>0&&!e||s.length>0&&!e||null!==a&&e||Boolean(n)&&!e&&(!this.hasAttribute("enable-recent-searches")||0===this.recentSearches.length)&&0===s.length)}formatSearchResult(e){if("string"==typeof e||"number"==typeof e)return String(e);if(e&&"object"==typeof e){const t=e;if("string"==typeof t.title)return t.title;if("string"==typeof t.label)return t.label}return String(e)}handleSearch(e){const t=(e??this.value).trim();t&&(this.dispatchEvent(new CustomEvent("db-search",{detail:{value:t},bubbles:!0,composed:!0})),this.hasAttribute("enable-recent-searches")&&(this.recentSearches=[t,...this.recentSearches.filter(e=>e!==t)].slice(0,10),this.saveRecentSearches(),this.updateDropdown()))}updateDropdown(){if(!this.dropdown)return;const s=this.shouldShowDropdown();if(this.dropdown.hidden=!s,this.classList.toggle("designbase-wc-search-bar--dropdown-open",s),!s)return this.dropdown.replaceChildren(),void this.detachDropdownPortal();this.dropdown.replaceChildren();const n=this.hasInputValue(),r=this.parseJsonArray("categories"),i=this.parseJsonArray("suggested-searches"),o=this.getAttribute("selected-category")||"",c=this.parseJsonArray("search-results"),h=this.getAttribute("empty-state-message"),d=a.includes(this.getAttribute("recent-searches-layout")||"vertical")&&this.getAttribute("recent-searches-layout")||"vertical";if(r.length>0){const e=document.createElement("div");e.className="designbase-wc-search-bar__categories",r.forEach(t=>{const s=document.createElement("db-chip");s.setAttribute("label",t),s.setAttribute("size","s"),s.setAttribute("variant",t===o?"primary":"default"),s.setAttribute("clickable",""),s.addEventListener("mousedown",e=>{e.preventDefault(),this.setAttribute("selected-category",t),this.dispatchEvent(new CustomEvent("db-category-change",{detail:{category:t},bubbles:!0,composed:!0})),this.updateDropdown()}),e.append(s)}),this.dropdown.append(e)}if(n&&this.hasAttribute("search-results")){const e=document.createElement("div");if(e.className="designbase-wc-search-bar__results",c.length>0){const t=document.createElement("ul");t.className="designbase-wc-search-bar__results-list",c.forEach((e,s)=>{const a=document.createElement("li");a.className="designbase-wc-search-bar__result-item",a.textContent=this.formatSearchResult(e),a.addEventListener("mousedown",t=>{t.preventDefault(),this.isFocused=!1,this.updateDropdown(),this.dispatchEvent(new CustomEvent("db-search-result-click",{detail:{result:e,index:s},bubbles:!0,composed:!0}))}),t.append(a)}),e.append(t)}else{const t=document.createElement("div");t.className="designbase-wc-search-bar__results-empty",t.textContent="검색 결과가 없습니다.",e.append(t)}return this.dropdown.append(e),void this.attachDropdownPortal()}if(this.hasAttribute("enable-recent-searches")&&!n&&this.recentSearches.length>0){const s=document.createElement("div");s.className=`designbase-wc-search-bar__recent-searches designbase-wc-search-bar__recent-searches--${d}`;const a=document.createElement("div");a.className="designbase-wc-search-bar__recent-header";const n=document.createElement("span");n.className="designbase-wc-search-bar__recent-title",n.textContent="최근 검색어";const r=document.createElement("button");r.type="button",r.className="designbase-wc-search-bar__clear-all-button",r.setAttribute("aria-label","모든 최근 검색어 삭제"),r.textContent="전체 삭제",r.addEventListener("mousedown",e=>{e.preventDefault(),this.recentSearches=[];try{localStorage.removeItem(this.getRecentSearchesKey())}catch{}this.updateDropdown()}),a.append(n,r);const i=document.createElement("div");i.className="designbase-wc-search-bar__recent-list",this.recentSearches.forEach(s=>{const a=document.createElement("div");a.className="designbase-wc-search-bar__recent-item";const n=document.createElement("button");n.type="button",n.className="designbase-wc-search-bar__recent-search-button",n.textContent=s,n.addEventListener("mousedown",e=>{if(e.preventDefault(),!this.input)return;this.input.value=s,this.setAttribute("value",s),this.syncValueState(),this.syncPlaceholder(),this.handleSearch(s);const t={value:s};this.dispatchEvent(new CustomEvent("db-input",{detail:t,bubbles:!0,composed:!0})),this.dispatchEvent(new CustomEvent("db-change",{detail:t,bubbles:!0,composed:!0})),this.isFocused=!1,this.updateDropdown()});const r=document.createElement("button");r.type="button",r.className="designbase-wc-search-bar__recent-remove-button",r.setAttribute("aria-label",`${s} 삭제`),r.append(t("close",e("s"))),r.addEventListener("mousedown",e=>{e.preventDefault(),this.recentSearches=this.recentSearches.filter(e=>e!==s),this.saveRecentSearches(),this.updateDropdown()}),a.append(n,r),i.append(a)}),s.append(a,i),this.dropdown.append(s)}if(i.length>0&&!n){const e=document.createElement("div");e.className="designbase-wc-search-bar__suggestions";const t=document.createElement("div");t.className="designbase-wc-search-bar__suggestions-header";const s=document.createElement("span");s.className="designbase-wc-search-bar__suggestions-title",s.textContent="추천 검색어",t.append(s);const a=document.createElement("div");a.className="designbase-wc-search-bar__suggestions-list",i.forEach((e,t)=>{const s=document.createElement("button");s.type="button",s.className=["designbase-wc-search-bar__suggestion-item",t===this.currentSuggestion&&"designbase-wc-search-bar__suggestion-item--active"].filter(Boolean).join(" "),s.textContent=e,s.addEventListener("mousedown",t=>{if(t.preventDefault(),!this.input)return;this.clearSuggestionInterval(),this.input.value=e,this.setAttribute("value",e),this.syncValueState(),this.syncPlaceholder(),this.handleSearch(e);const s={value:e};this.dispatchEvent(new CustomEvent("db-input",{detail:s,bubbles:!0,composed:!0})),this.dispatchEvent(new CustomEvent("db-change",{detail:s,bubbles:!0,composed:!0})),this.isFocused=!1,this.updateDropdown()}),a.append(s)}),e.append(t,a),this.dropdown.append(e)}if(h&&!n&&(!this.hasAttribute("enable-recent-searches")||0===this.recentSearches.length)&&0===i.length){const e=document.createElement("div");e.className="designbase-wc-search-bar__empty-state",e.textContent=h,this.dropdown.append(e)}this.attachDropdownPortal()}render(){const e=this.getAttribute("size")||"m",a=this.hasAttribute("disabled"),n=this.hasAttribute("readonly"),r=this.hasAttribute("full-width"),i=this.hasAttribute("show-shortcut"),o=this.getAttribute("value")??this.getAttribute("default-value")??"",c=Boolean(o.trim()),h=this.getIconPx();this.className=["designbase-wc-search-bar",`designbase-wc-search-bar--${s.includes(e)?e:"m"}`,a&&"designbase-wc-search-bar--disabled",n&&"designbase-wc-search-bar--readonly",r&&"designbase-wc-search-bar--full-width",c&&"designbase-wc-search-bar--has-value",this.shouldShowDropdown()&&"designbase-wc-search-bar--dropdown-open"].filter(Boolean).join(" "),this.setAttribute("role","search"),this.clearSuggestionInterval(),this.detachDropdownPortal(),this.replaceChildren();const d=document.createElement("div");d.className="designbase-wc-search-bar__container";const l=document.createElement("div");l.className="designbase-wc-search-bar__search-icon",l.setAttribute("aria-hidden","true"),l.append(t("search",h));const u=document.createElement("input");if(u.type="text",u.className="designbase-wc-search-bar__input",u.value=o,u.disabled=a,u.readOnly=n,u.setAttribute("aria-label","검색어 입력"),this.input=u,i){const t=document.createElement("db-badge");t.className="designbase-wc-search-bar__shortcut-badge",t.setAttribute("size","l"===e?"m":"s"),t.setAttribute("variant","secondary"),t.setAttribute("badge-style","text"),t.setAttribute("aria-hidden","true"),t.textContent=this.getAttribute("shortcut-label")||"⌘K",t.hidden=c||a||n,this.shortcutBadge=t,d.append(l,u,t)}else d.append(l,u);const b=document.createElement("button");b.type="button",b.className="designbase-wc-search-bar__clear-button",b.setAttribute("aria-label","검색어 지우기"),b.hidden=!c||a||n,b.append(t("close",h)),this.clearButton=b,d.append(b);const p=document.createElement("div");p.className="designbase-wc-search-bar__dropdown",p.hidden=!0,p.addEventListener("mousedown",e=>{e.preventDefault()}),this.dropdown=p,u.addEventListener("input",()=>{this.setAttribute("value",u.value),this.syncValueState(),this.syncPlaceholder(),this.updateDropdown(),this.syncSuggestionRolling();const e={value:u.value};this.dispatchEvent(new CustomEvent("db-input",{detail:e,bubbles:!0,composed:!0})),this.dispatchEvent(new CustomEvent("db-change",{detail:e,bubbles:!0,composed:!0}))}),u.addEventListener("keydown",e=>{"Enter"===e.key&&this.handleSearch()}),u.addEventListener("focus",()=>{this.isFocused=!0,this.updateDropdown(),this.dispatchEvent(new CustomEvent("db-focus",{bubbles:!0,composed:!0}))}),u.addEventListener("blur",()=>{this.isFocused=!1,this.blurTimer&&clearTimeout(this.blurTimer),this.updateDropdown(),this.dispatchEvent(new CustomEvent("db-blur",{bubbles:!0,composed:!0}))}),b.addEventListener("click",()=>{u.value="",this.setAttribute("value",""),this.syncValueState(),this.syncPlaceholder(),this.updateDropdown(),this.syncSuggestionRolling(),u.focus(),this.dispatchEvent(new CustomEvent("db-clear",{bubbles:!0,composed:!0}));const e={value:""};this.dispatchEvent(new CustomEvent("db-input",{detail:e,bubbles:!0,composed:!0})),this.dispatchEvent(new CustomEvent("db-change",{detail:e,bubbles:!0,composed:!0}))}),this.append(d,p),this.syncIcons(),this.syncPlaceholder(),this.syncSuggestionRolling(),this.bindGlobalShortcut(),this.updateDropdown()}}customElements.get("db-search-bar")||customElements.define("db-search-bar",DbSearchBar);export{DbSearchBar};
1
+ import { getDbComponentIconSize, createDbIcon } from './db-icons.js';
2
+
3
+ const SIZES = ['s', 'm', 'l'];
4
+ const RECENT_LAYOUTS = ['vertical', 'horizontal'];
5
+ class DbSearchBar extends HTMLElement {
6
+ constructor() {
7
+ super(...arguments);
8
+ this.recentSearches = [];
9
+ this.isFocused = false;
10
+ this.currentSuggestion = 0;
11
+ this.scrollParents = [];
12
+ }
13
+ static get observedAttributes() {
14
+ return [
15
+ 'value',
16
+ 'default-value',
17
+ 'placeholder',
18
+ 'size',
19
+ 'disabled',
20
+ 'readonly',
21
+ 'full-width',
22
+ 'show-shortcut',
23
+ 'shortcut-label',
24
+ 'enable-recent-searches',
25
+ 'recent-searches-key',
26
+ 'recent-searches-layout',
27
+ 'suggested-searches',
28
+ 'suggestion-rolling-interval',
29
+ 'categories',
30
+ 'selected-category',
31
+ 'search-results',
32
+ 'empty-state-message',
33
+ ];
34
+ }
35
+ getResolvedSize() {
36
+ const size = this.getAttribute('size') || 'm';
37
+ return SIZES.includes(size) ? size : 'm';
38
+ }
39
+ getIconPx() {
40
+ return getDbComponentIconSize(this.getResolvedSize());
41
+ }
42
+ syncIcons() {
43
+ const iconPx = this.getIconPx();
44
+ this.querySelector('.designbase-wc-search-bar__search-icon')?.replaceChildren(createDbIcon('search', iconPx));
45
+ this.clearButton?.replaceChildren(createDbIcon('close', iconPx));
46
+ }
47
+ hasInputValue() {
48
+ return Boolean((this.input?.value ?? this.getAttribute('value') ?? '').trim());
49
+ }
50
+ get value() {
51
+ return this.input?.value ?? this.getAttribute('value') ?? '';
52
+ }
53
+ set value(nextValue) {
54
+ this.setAttribute('value', nextValue);
55
+ if (this.input)
56
+ this.input.value = nextValue;
57
+ }
58
+ connectedCallback() {
59
+ this.loadRecentSearches();
60
+ this.render();
61
+ this.bindGlobalShortcut();
62
+ }
63
+ disconnectedCallback() {
64
+ this.clearSuggestionInterval();
65
+ this.unbindGlobalShortcut();
66
+ this.detachDropdownPortal();
67
+ if (this.blurTimer)
68
+ clearTimeout(this.blurTimer);
69
+ }
70
+ attributeChangedCallback(name) {
71
+ if (!this.isConnected)
72
+ return;
73
+ if (name === 'value' && this.input) {
74
+ this.input.value = this.getAttribute('value') || '';
75
+ this.syncValueState();
76
+ this.syncPlaceholder();
77
+ this.updateDropdown();
78
+ this.syncSuggestionRolling();
79
+ return;
80
+ }
81
+ if (name === 'disabled' ||
82
+ name === 'readonly' ||
83
+ name === 'placeholder' ||
84
+ name === 'show-shortcut' ||
85
+ name === 'shortcut-label' ||
86
+ name === 'selected-category' ||
87
+ name === 'search-results' ||
88
+ name === 'empty-state-message' ||
89
+ name === 'suggested-searches') {
90
+ this.syncAttributes();
91
+ this.syncPlaceholder();
92
+ this.updateDropdown();
93
+ this.syncSuggestionRolling();
94
+ return;
95
+ }
96
+ if (name === 'enable-recent-searches' || name === 'recent-searches-key') {
97
+ this.loadRecentSearches();
98
+ this.updateDropdown();
99
+ return;
100
+ }
101
+ if (name === 'categories' || name === 'recent-searches-layout') {
102
+ this.updateDropdown();
103
+ return;
104
+ }
105
+ this.render();
106
+ }
107
+ focus() {
108
+ this.input?.focus();
109
+ }
110
+ parseJsonArray(attributeName) {
111
+ try {
112
+ const parsed = JSON.parse(this.getAttribute(attributeName) || '[]');
113
+ return Array.isArray(parsed) ? parsed : [];
114
+ }
115
+ catch {
116
+ return [];
117
+ }
118
+ }
119
+ getRecentSearchesKey() {
120
+ return this.getAttribute('recent-searches-key') || 'searchbar-recent-searches';
121
+ }
122
+ loadRecentSearches() {
123
+ if (!this.hasAttribute('enable-recent-searches')) {
124
+ this.recentSearches = [];
125
+ return;
126
+ }
127
+ try {
128
+ const stored = localStorage.getItem(this.getRecentSearchesKey());
129
+ this.recentSearches = stored ? JSON.parse(stored) : [];
130
+ if (!Array.isArray(this.recentSearches))
131
+ this.recentSearches = [];
132
+ }
133
+ catch {
134
+ this.recentSearches = [];
135
+ }
136
+ }
137
+ saveRecentSearches() {
138
+ try {
139
+ localStorage.setItem(this.getRecentSearchesKey(), JSON.stringify(this.recentSearches));
140
+ }
141
+ catch {
142
+ // ignore storage errors
143
+ }
144
+ }
145
+ clearSuggestionInterval() {
146
+ if (this.suggestionInterval) {
147
+ clearInterval(this.suggestionInterval);
148
+ this.suggestionInterval = undefined;
149
+ }
150
+ }
151
+ syncSuggestionRolling() {
152
+ this.clearSuggestionInterval();
153
+ const suggestions = this.parseJsonArray('suggested-searches');
154
+ const hasValue = this.hasInputValue();
155
+ const interval = Number(this.getAttribute('suggestion-rolling-interval') || 5000);
156
+ if (suggestions.length > 0 && !hasValue) {
157
+ this.suggestionInterval = setInterval(() => {
158
+ this.currentSuggestion = (this.currentSuggestion + 1) % suggestions.length;
159
+ this.syncPlaceholder();
160
+ this.updateDropdown();
161
+ }, interval);
162
+ }
163
+ }
164
+ syncPlaceholder() {
165
+ if (!this.input)
166
+ return;
167
+ const suggestions = this.parseJsonArray('suggested-searches');
168
+ const hasValue = this.hasInputValue();
169
+ const placeholder = this.getAttribute('placeholder') || '검색...';
170
+ if (suggestions.length > 0 && !hasValue) {
171
+ this.input.placeholder = suggestions[this.currentSuggestion] || placeholder;
172
+ }
173
+ else {
174
+ this.input.placeholder = placeholder;
175
+ }
176
+ }
177
+ syncValueState() {
178
+ const hasValue = this.hasInputValue();
179
+ this.classList.toggle('designbase-wc-search-bar--has-value', hasValue);
180
+ if (this.clearButton) {
181
+ this.clearButton.hidden = !hasValue || this.hasAttribute('disabled') || this.hasAttribute('readonly');
182
+ }
183
+ if (this.shortcutBadge) {
184
+ this.shortcutBadge.hidden =
185
+ !this.hasAttribute('show-shortcut') ||
186
+ hasValue ||
187
+ this.hasAttribute('disabled') ||
188
+ this.hasAttribute('readonly');
189
+ }
190
+ this.syncIcons();
191
+ }
192
+ syncAttributes() {
193
+ const size = this.getAttribute('size') || 'm';
194
+ const disabled = this.hasAttribute('disabled');
195
+ const readOnly = this.hasAttribute('readonly');
196
+ const showShortcut = this.hasAttribute('show-shortcut');
197
+ this.className = [
198
+ 'designbase-wc-search-bar',
199
+ `designbase-wc-search-bar--${SIZES.includes(size) ? size : 'm'}`,
200
+ disabled && 'designbase-wc-search-bar--disabled',
201
+ readOnly && 'designbase-wc-search-bar--readonly',
202
+ this.hasAttribute('full-width') && 'designbase-wc-search-bar--full-width',
203
+ Boolean(this.hasInputValue()) && 'designbase-wc-search-bar--has-value',
204
+ ]
205
+ .filter(Boolean)
206
+ .join(' ');
207
+ if (this.input) {
208
+ this.input.disabled = disabled;
209
+ this.input.readOnly = readOnly;
210
+ }
211
+ if (this.shortcutBadge) {
212
+ this.shortcutBadge.hidden = !showShortcut || this.hasInputValue() || disabled || readOnly;
213
+ this.shortcutBadge.textContent = this.getAttribute('shortcut-label') || '⌘K';
214
+ }
215
+ this.syncValueState();
216
+ }
217
+ bindGlobalShortcut() {
218
+ this.unbindGlobalShortcut();
219
+ if (!this.hasAttribute('show-shortcut') || this.hasAttribute('disabled') || this.hasAttribute('readonly')) {
220
+ return;
221
+ }
222
+ this.globalKeyHandler = (event) => {
223
+ const isModifier = event.metaKey || event.ctrlKey;
224
+ let keyToMatch = 'k';
225
+ const shortcutLabel = this.getAttribute('shortcut-label') || '⌘K';
226
+ const lastChar = shortcutLabel.slice(-1).toLowerCase();
227
+ if (lastChar)
228
+ keyToMatch = lastChar;
229
+ if (isModifier && event.key.toLowerCase() === keyToMatch) {
230
+ event.preventDefault();
231
+ this.focus();
232
+ }
233
+ };
234
+ window.addEventListener('keydown', this.globalKeyHandler);
235
+ }
236
+ unbindGlobalShortcut() {
237
+ if (this.globalKeyHandler) {
238
+ window.removeEventListener('keydown', this.globalKeyHandler);
239
+ this.globalKeyHandler = undefined;
240
+ }
241
+ }
242
+ positionDropdown() {
243
+ if (!this.dropdown || this.dropdown.hidden)
244
+ return;
245
+ const hostRect = this.getBoundingClientRect();
246
+ const inputRect = this.input?.getBoundingClientRect();
247
+ const anchorRect = inputRect ?? hostRect;
248
+ this.dropdown.style.top = `${anchorRect.bottom + 4}px`;
249
+ this.dropdown.style.left = `${hostRect.left}px`;
250
+ this.dropdown.style.width = `${hostRect.width}px`;
251
+ this.dropdown.style.right = 'auto';
252
+ }
253
+ bindRepositionListeners() {
254
+ if (this.repositionHandler)
255
+ return;
256
+ this.repositionHandler = () => {
257
+ requestAnimationFrame(() => this.positionDropdown());
258
+ };
259
+ window.addEventListener('scroll', this.repositionHandler, true);
260
+ window.addEventListener('resize', this.repositionHandler);
261
+ this.scrollParents = [];
262
+ let parent = this.parentElement;
263
+ while (parent && parent !== document.body) {
264
+ const style = getComputedStyle(parent);
265
+ const scrollableY = /(auto|scroll|overlay)/.test(style.overflowY);
266
+ const scrollable = /(auto|scroll|overlay)/.test(style.overflow);
267
+ if ((scrollableY || scrollable) && parent.scrollHeight > parent.clientHeight) {
268
+ parent.addEventListener('scroll', this.repositionHandler, { passive: true });
269
+ this.scrollParents.push(parent);
270
+ }
271
+ parent = parent.parentElement;
272
+ }
273
+ }
274
+ unbindRepositionListeners() {
275
+ if (!this.repositionHandler)
276
+ return;
277
+ window.removeEventListener('scroll', this.repositionHandler, true);
278
+ window.removeEventListener('resize', this.repositionHandler);
279
+ this.scrollParents.forEach((parent) => parent.removeEventListener('scroll', this.repositionHandler));
280
+ this.scrollParents = [];
281
+ this.repositionHandler = undefined;
282
+ }
283
+ attachDropdownPortal() {
284
+ if (!this.dropdown || this.dropdown.hidden)
285
+ return;
286
+ this.dropdown.classList.add('designbase-wc-search-bar__dropdown--portal');
287
+ if (this.dropdown.parentElement !== document.body) {
288
+ document.body.append(this.dropdown);
289
+ }
290
+ requestAnimationFrame(() => this.positionDropdown());
291
+ if (!this.repositionHandler) {
292
+ this.bindRepositionListeners();
293
+ }
294
+ }
295
+ detachDropdownPortal() {
296
+ this.unbindRepositionListeners();
297
+ if (!this.dropdown)
298
+ return;
299
+ this.dropdown.classList.remove('designbase-wc-search-bar__dropdown--portal');
300
+ this.dropdown.style.top = '';
301
+ this.dropdown.style.left = '';
302
+ this.dropdown.style.width = '';
303
+ this.dropdown.style.right = '';
304
+ if (this.dropdown.parentElement === document.body && this.isConnected) {
305
+ this.append(this.dropdown);
306
+ }
307
+ }
308
+ shouldShowDropdown() {
309
+ const hasValue = this.hasInputValue();
310
+ const categories = this.parseJsonArray('categories');
311
+ const suggestions = this.parseJsonArray('suggested-searches');
312
+ const searchResults = this.getAttribute('search-results');
313
+ const emptyStateMessage = this.getAttribute('empty-state-message');
314
+ return (this.isFocused &&
315
+ (categories.length > 0 ||
316
+ (this.hasAttribute('enable-recent-searches') && this.recentSearches.length > 0 && !hasValue) ||
317
+ (suggestions.length > 0 && !hasValue) ||
318
+ (searchResults !== null && hasValue) ||
319
+ (Boolean(emptyStateMessage) &&
320
+ !hasValue &&
321
+ (!this.hasAttribute('enable-recent-searches') || this.recentSearches.length === 0) &&
322
+ suggestions.length === 0)));
323
+ }
324
+ formatSearchResult(result) {
325
+ if (typeof result === 'string' || typeof result === 'number')
326
+ return String(result);
327
+ if (result && typeof result === 'object') {
328
+ const record = result;
329
+ if (typeof record.title === 'string')
330
+ return record.title;
331
+ if (typeof record.label === 'string')
332
+ return record.label;
333
+ }
334
+ return String(result);
335
+ }
336
+ handleSearch(searchValue) {
337
+ const searchTerm = (searchValue ?? this.value).trim();
338
+ if (!searchTerm)
339
+ return;
340
+ this.dispatchEvent(new CustomEvent('db-search', {
341
+ detail: { value: searchTerm },
342
+ bubbles: true,
343
+ composed: true,
344
+ }));
345
+ if (this.hasAttribute('enable-recent-searches')) {
346
+ this.recentSearches = [searchTerm, ...this.recentSearches.filter((item) => item !== searchTerm)].slice(0, 10);
347
+ this.saveRecentSearches();
348
+ this.updateDropdown();
349
+ }
350
+ }
351
+ updateDropdown() {
352
+ if (!this.dropdown)
353
+ return;
354
+ const show = this.shouldShowDropdown();
355
+ this.dropdown.hidden = !show;
356
+ this.classList.toggle('designbase-wc-search-bar--dropdown-open', show);
357
+ if (!show) {
358
+ this.dropdown.replaceChildren();
359
+ this.detachDropdownPortal();
360
+ return;
361
+ }
362
+ this.dropdown.replaceChildren();
363
+ const hasValue = this.hasInputValue();
364
+ const categories = this.parseJsonArray('categories');
365
+ const suggestions = this.parseJsonArray('suggested-searches');
366
+ const selectedCategory = this.getAttribute('selected-category') || '';
367
+ const searchResults = this.parseJsonArray('search-results');
368
+ const emptyStateMessage = this.getAttribute('empty-state-message');
369
+ const recentLayout = RECENT_LAYOUTS.includes((this.getAttribute('recent-searches-layout') || 'vertical'))
370
+ ? this.getAttribute('recent-searches-layout') || 'vertical'
371
+ : 'vertical';
372
+ if (categories.length > 0) {
373
+ const categoriesEl = document.createElement('div');
374
+ categoriesEl.className = 'designbase-wc-search-bar__categories';
375
+ categories.forEach((category) => {
376
+ const chip = document.createElement('db-chip');
377
+ chip.setAttribute('label', category);
378
+ chip.setAttribute('size', 's');
379
+ chip.setAttribute('variant', category === selectedCategory ? 'primary' : 'default');
380
+ chip.setAttribute('clickable', '');
381
+ chip.addEventListener('mousedown', (event) => {
382
+ event.preventDefault();
383
+ this.setAttribute('selected-category', category);
384
+ this.dispatchEvent(new CustomEvent('db-category-change', {
385
+ detail: { category },
386
+ bubbles: true,
387
+ composed: true,
388
+ }));
389
+ this.updateDropdown();
390
+ });
391
+ categoriesEl.append(chip);
392
+ });
393
+ this.dropdown.append(categoriesEl);
394
+ }
395
+ if (hasValue && this.hasAttribute('search-results')) {
396
+ const results = document.createElement('div');
397
+ results.className = 'designbase-wc-search-bar__results';
398
+ if (searchResults.length > 0) {
399
+ const list = document.createElement('ul');
400
+ list.className = 'designbase-wc-search-bar__results-list';
401
+ searchResults.forEach((result, index) => {
402
+ const item = document.createElement('li');
403
+ item.className = 'designbase-wc-search-bar__result-item';
404
+ item.textContent = this.formatSearchResult(result);
405
+ item.addEventListener('mousedown', (event) => {
406
+ event.preventDefault();
407
+ this.isFocused = false;
408
+ this.updateDropdown();
409
+ this.dispatchEvent(new CustomEvent('db-search-result-click', {
410
+ detail: { result, index },
411
+ bubbles: true,
412
+ composed: true,
413
+ }));
414
+ });
415
+ list.append(item);
416
+ });
417
+ results.append(list);
418
+ }
419
+ else {
420
+ const empty = document.createElement('div');
421
+ empty.className = 'designbase-wc-search-bar__results-empty';
422
+ empty.textContent = '검색 결과가 없습니다.';
423
+ results.append(empty);
424
+ }
425
+ this.dropdown.append(results);
426
+ this.attachDropdownPortal();
427
+ return;
428
+ }
429
+ if (this.hasAttribute('enable-recent-searches') && !hasValue && this.recentSearches.length > 0) {
430
+ const recent = document.createElement('div');
431
+ recent.className = `designbase-wc-search-bar__recent-searches designbase-wc-search-bar__recent-searches--${recentLayout}`;
432
+ const header = document.createElement('div');
433
+ header.className = 'designbase-wc-search-bar__recent-header';
434
+ const title = document.createElement('span');
435
+ title.className = 'designbase-wc-search-bar__recent-title';
436
+ title.textContent = '최근 검색어';
437
+ const clearAll = document.createElement('button');
438
+ clearAll.type = 'button';
439
+ clearAll.className = 'designbase-wc-search-bar__clear-all-button';
440
+ clearAll.setAttribute('aria-label', '모든 최근 검색어 삭제');
441
+ clearAll.textContent = '전체 삭제';
442
+ clearAll.addEventListener('mousedown', (event) => {
443
+ event.preventDefault();
444
+ this.recentSearches = [];
445
+ try {
446
+ localStorage.removeItem(this.getRecentSearchesKey());
447
+ }
448
+ catch {
449
+ // ignore
450
+ }
451
+ this.updateDropdown();
452
+ });
453
+ header.append(title, clearAll);
454
+ const list = document.createElement('div');
455
+ list.className = 'designbase-wc-search-bar__recent-list';
456
+ this.recentSearches.forEach((searchTerm) => {
457
+ const item = document.createElement('div');
458
+ item.className = 'designbase-wc-search-bar__recent-item';
459
+ const searchButton = document.createElement('button');
460
+ searchButton.type = 'button';
461
+ searchButton.className = 'designbase-wc-search-bar__recent-search-button';
462
+ searchButton.textContent = searchTerm;
463
+ searchButton.addEventListener('mousedown', (event) => {
464
+ event.preventDefault();
465
+ if (!this.input)
466
+ return;
467
+ this.input.value = searchTerm;
468
+ this.setAttribute('value', searchTerm);
469
+ this.syncValueState();
470
+ this.syncPlaceholder();
471
+ this.handleSearch(searchTerm);
472
+ const detail = { value: searchTerm };
473
+ this.dispatchEvent(new CustomEvent('db-input', { detail, bubbles: true, composed: true }));
474
+ this.dispatchEvent(new CustomEvent('db-change', { detail, bubbles: true, composed: true }));
475
+ this.isFocused = false;
476
+ this.updateDropdown();
477
+ });
478
+ const removeButton = document.createElement('button');
479
+ removeButton.type = 'button';
480
+ removeButton.className = 'designbase-wc-search-bar__recent-remove-button';
481
+ removeButton.setAttribute('aria-label', `${searchTerm} 삭제`);
482
+ removeButton.append(createDbIcon('close', getDbComponentIconSize('s')));
483
+ removeButton.addEventListener('mousedown', (event) => {
484
+ event.preventDefault();
485
+ this.recentSearches = this.recentSearches.filter((item) => item !== searchTerm);
486
+ this.saveRecentSearches();
487
+ this.updateDropdown();
488
+ });
489
+ item.append(searchButton, removeButton);
490
+ list.append(item);
491
+ });
492
+ recent.append(header, list);
493
+ this.dropdown.append(recent);
494
+ }
495
+ if (suggestions.length > 0 && !hasValue) {
496
+ const suggestionsEl = document.createElement('div');
497
+ suggestionsEl.className = 'designbase-wc-search-bar__suggestions';
498
+ const header = document.createElement('div');
499
+ header.className = 'designbase-wc-search-bar__suggestions-header';
500
+ const title = document.createElement('span');
501
+ title.className = 'designbase-wc-search-bar__suggestions-title';
502
+ title.textContent = '추천 검색어';
503
+ header.append(title);
504
+ const list = document.createElement('div');
505
+ list.className = 'designbase-wc-search-bar__suggestions-list';
506
+ suggestions.forEach((suggestion, index) => {
507
+ const button = document.createElement('button');
508
+ button.type = 'button';
509
+ button.className = [
510
+ 'designbase-wc-search-bar__suggestion-item',
511
+ index === this.currentSuggestion && 'designbase-wc-search-bar__suggestion-item--active',
512
+ ]
513
+ .filter(Boolean)
514
+ .join(' ');
515
+ button.textContent = suggestion;
516
+ button.addEventListener('mousedown', (event) => {
517
+ event.preventDefault();
518
+ if (!this.input)
519
+ return;
520
+ this.clearSuggestionInterval();
521
+ this.input.value = suggestion;
522
+ this.setAttribute('value', suggestion);
523
+ this.syncValueState();
524
+ this.syncPlaceholder();
525
+ this.handleSearch(suggestion);
526
+ const detail = { value: suggestion };
527
+ this.dispatchEvent(new CustomEvent('db-input', { detail, bubbles: true, composed: true }));
528
+ this.dispatchEvent(new CustomEvent('db-change', { detail, bubbles: true, composed: true }));
529
+ this.isFocused = false;
530
+ this.updateDropdown();
531
+ });
532
+ list.append(button);
533
+ });
534
+ suggestionsEl.append(header, list);
535
+ this.dropdown.append(suggestionsEl);
536
+ }
537
+ if (emptyStateMessage &&
538
+ !hasValue &&
539
+ (!this.hasAttribute('enable-recent-searches') || this.recentSearches.length === 0) &&
540
+ suggestions.length === 0) {
541
+ const empty = document.createElement('div');
542
+ empty.className = 'designbase-wc-search-bar__empty-state';
543
+ empty.textContent = emptyStateMessage;
544
+ this.dropdown.append(empty);
545
+ }
546
+ this.attachDropdownPortal();
547
+ }
548
+ render() {
549
+ const size = this.getAttribute('size') || 'm';
550
+ const disabled = this.hasAttribute('disabled');
551
+ const readOnly = this.hasAttribute('readonly');
552
+ const fullWidth = this.hasAttribute('full-width');
553
+ const showShortcut = this.hasAttribute('show-shortcut');
554
+ const initialValue = this.getAttribute('value') ?? this.getAttribute('default-value') ?? '';
555
+ const hasInitialValue = Boolean(initialValue.trim());
556
+ const iconPx = this.getIconPx();
557
+ this.className = [
558
+ 'designbase-wc-search-bar',
559
+ `designbase-wc-search-bar--${SIZES.includes(size) ? size : 'm'}`,
560
+ disabled && 'designbase-wc-search-bar--disabled',
561
+ readOnly && 'designbase-wc-search-bar--readonly',
562
+ fullWidth && 'designbase-wc-search-bar--full-width',
563
+ hasInitialValue && 'designbase-wc-search-bar--has-value',
564
+ this.shouldShowDropdown() && 'designbase-wc-search-bar--dropdown-open',
565
+ ]
566
+ .filter(Boolean)
567
+ .join(' ');
568
+ this.setAttribute('role', 'search');
569
+ this.clearSuggestionInterval();
570
+ this.detachDropdownPortal();
571
+ this.replaceChildren();
572
+ const container = document.createElement('div');
573
+ container.className = 'designbase-wc-search-bar__container';
574
+ const searchIcon = document.createElement('div');
575
+ searchIcon.className = 'designbase-wc-search-bar__search-icon';
576
+ searchIcon.setAttribute('aria-hidden', 'true');
577
+ searchIcon.append(createDbIcon('search', iconPx));
578
+ const input = document.createElement('input');
579
+ input.type = 'text';
580
+ input.className = 'designbase-wc-search-bar__input';
581
+ input.value = initialValue;
582
+ input.disabled = disabled;
583
+ input.readOnly = readOnly;
584
+ input.setAttribute('aria-label', '검색어 입력');
585
+ this.input = input;
586
+ if (showShortcut) {
587
+ const shortcutBadge = document.createElement('db-badge');
588
+ shortcutBadge.className = 'designbase-wc-search-bar__shortcut-badge';
589
+ shortcutBadge.setAttribute('size', size === 'l' ? 'm' : 's');
590
+ shortcutBadge.setAttribute('variant', 'secondary');
591
+ shortcutBadge.setAttribute('badge-style', 'text');
592
+ shortcutBadge.setAttribute('aria-hidden', 'true');
593
+ shortcutBadge.textContent = this.getAttribute('shortcut-label') || '⌘K';
594
+ shortcutBadge.hidden = hasInitialValue || disabled || readOnly;
595
+ this.shortcutBadge = shortcutBadge;
596
+ container.append(searchIcon, input, shortcutBadge);
597
+ }
598
+ else {
599
+ container.append(searchIcon, input);
600
+ }
601
+ const clearButton = document.createElement('button');
602
+ clearButton.type = 'button';
603
+ clearButton.className = 'designbase-wc-search-bar__clear-button';
604
+ clearButton.setAttribute('aria-label', '검색어 지우기');
605
+ clearButton.hidden = !hasInitialValue || disabled || readOnly;
606
+ clearButton.append(createDbIcon('close', iconPx));
607
+ this.clearButton = clearButton;
608
+ container.append(clearButton);
609
+ const dropdown = document.createElement('div');
610
+ dropdown.className = 'designbase-wc-search-bar__dropdown';
611
+ dropdown.hidden = true;
612
+ dropdown.addEventListener('mousedown', (event) => {
613
+ event.preventDefault();
614
+ });
615
+ this.dropdown = dropdown;
616
+ input.addEventListener('input', () => {
617
+ this.setAttribute('value', input.value);
618
+ this.syncValueState();
619
+ this.syncPlaceholder();
620
+ this.updateDropdown();
621
+ this.syncSuggestionRolling();
622
+ const detail = { value: input.value };
623
+ this.dispatchEvent(new CustomEvent('db-input', { detail, bubbles: true, composed: true }));
624
+ this.dispatchEvent(new CustomEvent('db-change', { detail, bubbles: true, composed: true }));
625
+ });
626
+ input.addEventListener('keydown', (event) => {
627
+ if (event.key === 'Enter') {
628
+ this.handleSearch();
629
+ }
630
+ });
631
+ input.addEventListener('focus', () => {
632
+ this.isFocused = true;
633
+ this.updateDropdown();
634
+ this.dispatchEvent(new CustomEvent('db-focus', { bubbles: true, composed: true }));
635
+ });
636
+ input.addEventListener('blur', () => {
637
+ this.isFocused = false;
638
+ if (this.blurTimer)
639
+ clearTimeout(this.blurTimer);
640
+ this.updateDropdown();
641
+ this.dispatchEvent(new CustomEvent('db-blur', { bubbles: true, composed: true }));
642
+ });
643
+ clearButton.addEventListener('click', () => {
644
+ input.value = '';
645
+ this.setAttribute('value', '');
646
+ this.syncValueState();
647
+ this.syncPlaceholder();
648
+ this.updateDropdown();
649
+ this.syncSuggestionRolling();
650
+ input.focus();
651
+ this.dispatchEvent(new CustomEvent('db-clear', { bubbles: true, composed: true }));
652
+ const detail = { value: '' };
653
+ this.dispatchEvent(new CustomEvent('db-input', { detail, bubbles: true, composed: true }));
654
+ this.dispatchEvent(new CustomEvent('db-change', { detail, bubbles: true, composed: true }));
655
+ });
656
+ this.append(container, dropdown);
657
+ this.syncIcons();
658
+ this.syncPlaceholder();
659
+ this.syncSuggestionRolling();
660
+ this.bindGlobalShortcut();
661
+ this.updateDropdown();
662
+ }
663
+ }
664
+ if (!customElements.get('db-search-bar')) {
665
+ customElements.define('db-search-bar', DbSearchBar);
666
+ }
667
+
668
+ export { DbSearchBar };