{"version":3,"file":"ngxtension-host-binding.mjs","sources":["../../../../libs/ngxtension/host-binding/src/host-binding.ts","../../../../libs/ngxtension/host-binding/src/ngxtension-host-binding.ts"],"sourcesContent":["/* eslint-disable @typescript-eslint/ban-types */\nimport {\n\teffect,\n\tElementRef,\n\tHostBinding,\n\tinject,\n\tInjector,\n\tRenderer2,\n\tRendererStyleFlags2,\n\trunInInjectionContext,\n\ttype Signal,\n\ttype WritableSignal,\n} from '@angular/core';\nimport { assertInjector } from 'ngxtension/assert-injector';\n\n/**\n * `hostBinding` takes a `hostPropertyName` to attach a data property, a class, a style or an attribute (as `@HostBinding` would) to the host.\n * The udpate is applied based on the update of the provided signal (writable or not).\n *\n * @param {Required<HostBinding>['hostPropertyName']} hostPropertyName - the same property that is bound to a data property, a class, a style or an attribute as `@HostBinding`.\n * @param {Signal | WritableSignal} signal - the signal on which to react to changes to update the host, and the one that will be returned as it is\n * @returns {Signal | WritableSignal}\n *\n * @example\n * ```ts\n * export class MyComponent {\n *  readonly background = hostBinding('style.background', signal('blue'));\n *\n *  constructor() {\n *    setTimeout(() => this.background.set('red'), 3000);\n *  }\n * }\n * ```\n */\nexport function hostBinding<T, S extends Signal<T> | WritableSignal<T>>(\n\thostPropertyName: Required<HostBinding>['hostPropertyName'],\n\tsignal: S,\n\tinjector?: Injector,\n): S {\n\tinjector = assertInjector(hostBinding, injector);\n\n\trunInInjectionContext(injector, () => {\n\t\tconst renderer = inject(Renderer2);\n\t\tconst element: HTMLElement = inject(ElementRef).nativeElement;\n\n\t\tlet prevClasses: string[] = [];\n\n\t\teffect(() => {\n\t\t\tconst value = signal();\n\t\t\tconst [binding, property, unit] = hostPropertyName.split('.');\n\n\t\t\tswitch (binding) {\n\t\t\t\tcase 'style':\n\t\t\t\t\tif (property) {\n\t\t\t\t\t\trenderer.setStyle(\n\t\t\t\t\t\t\telement,\n\t\t\t\t\t\t\tproperty,\n\t\t\t\t\t\t\t`${value}${unit ?? ''}`,\n\t\t\t\t\t\t\tproperty.startsWith('--')\n\t\t\t\t\t\t\t\t? RendererStyleFlags2.DashCase\n\t\t\t\t\t\t\t\t: undefined,\n\t\t\t\t\t\t);\n\t\t\t\t\t} else if (typeof value === 'object' && value !== null) {\n\t\t\t\t\t\tfor (const [style, val] of Object.entries(value)) {\n\t\t\t\t\t\t\trenderer.setStyle(\n\t\t\t\t\t\t\t\telement,\n\t\t\t\t\t\t\t\tstyle,\n\t\t\t\t\t\t\t\tval,\n\t\t\t\t\t\t\t\tstyle.startsWith('--')\n\t\t\t\t\t\t\t\t\t? RendererStyleFlags2.DashCase\n\t\t\t\t\t\t\t\t\t: undefined,\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tbreak;\n\t\t\t\tcase 'attr':\n\t\t\t\t\tif (value == null) {\n\t\t\t\t\t\trenderer.removeAttribute(element, property);\n\t\t\t\t\t} else {\n\t\t\t\t\t\trenderer.setAttribute(element, property, String(value));\n\t\t\t\t\t}\n\t\t\t\t\tbreak;\n\t\t\t\tcase 'class':\n\t\t\t\t\tif (!property) {\n\t\t\t\t\t\tif (prevClasses.length) {\n\t\t\t\t\t\t\tprevClasses.forEach((k) => renderer.removeClass(element, k));\n\t\t\t\t\t\t}\n\t\t\t\t\t\tprevClasses =\n\t\t\t\t\t\t\ttypeof value === 'string' ? value.split(' ').filter(Boolean) : [];\n\t\t\t\t\t\tprevClasses.forEach((k) => renderer.addClass(element, k));\n\t\t\t\t\t} else {\n\t\t\t\t\t\tif (value) {\n\t\t\t\t\t\t\trenderer.addClass(element, property);\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\trenderer.removeClass(element, property);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tbreak;\n\t\t\t\tdefault:\n\t\t\t\t\trenderer.setProperty(element, binding, value);\n\t\t\t}\n\t\t});\n\t});\n\n\treturn signal;\n}\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './index';\n"],"names":[],"m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