{"version":3,"sources":["../src/override.ts","../src/replace.ts","../src/slots.tsx","../src/contains.ts"],"names":["override","useDataAttributes","withForwardRef","SlottedComponent","useContext","Box","mergedFnSlot","props","mergeProps","useDeepCompareMemo","React","memo","BentoError"],"mappings":";;;;;;;;;;;;;;;AAWA,SAAS,cAAc,GAAA,EAAa;AAClC,EAAA,OAAO,GAAA,CAAI,WAAW,IAAI,CAAA;AAC5B;AAkCA,IAAM,QAAA,GAAqB,CAAC,WAAA,EAAa,OAAO,CAAA;AAUzC,SAAS,QAAA,CAA4C;AAAA,EAC1D,OAAA;AAAA,EACA;AACF,CAAA,EAAoD;AAClD,EAAA,MAAM,SAAmB,EAAC;AAC1B,EAAA,MAAM,EAAE,SAAA,EAAW,QAAA,EAAU,QAAA,EAAAA,SAAAA,KAAa,OAAA,CAAQ,KAAA;AAClD,EAAA,MAAM,IAAA,GAAwC,QAAA,CAAS,SAAA,CAAU,IAAA,CAAK,GAAG,CAAC,CAAA;AAE1E,EAAA,IAAI,OAAO,KAAA,CAAM,eAAe,CAAA,KAAM,QAAA,EAAU;AAC9C,IAAA,MAAA,CAAO,KAAK,GAAG,KAAA,CAAM,eAAe,CAAA,CAAE,KAAA,CAAM,GAAG,CAAC,CAAA;AAAA,EAClD;AAEA,EAAA,IAAIA,SAAAA,IAAY,CAAC,MAAA,CAAO,QAAA,CAAS,SAAS,CAAA,EAAG,MAAA,CAAO,KAAK,SAAS,CAAA;AAClE,EAAA,IAAI,WAAA,IAAe,SAAS,CAAC,MAAA,CAAO,SAAS,WAAW,CAAA,EAAG,MAAA,CAAO,IAAA,CAAK,WAAW,CAAA;AAOlF,EAAA,IAAI,WAAW,KAAA,IAAS,CAAC,MAAA,CAAO,QAAA,CAAS,OAAO,CAAA,EAAG;AACjD,IAAA,MAAM,QAAQ,KAAA,CAAM,KAAA;AACpB,IAAA,MAAM,IAAA,GAAO,MAAA,CAAO,IAAA,CAAK,KAAK,CAAA;AAE9B,IAAA,IAAI,IAAA,CAAK,KAA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,CAAA;AACvD","file":"index.cjs","sourcesContent":["import { type ComponentType, type CSSProperties } from 'react';\nimport { useDataAttributes } from '@bento/use-data-attributes';\nimport { type BoxContext } from '@bento/box';\n\n/**\n * Checks if a given key is a CSS variable.\n *\n * @param key - The key to check.\n * @returns true if the key is a CSS variable, otherwise false.\n * @private\n */\nfunction isCSSVariable(key: string) {\n  return key.startsWith('--');\n}\n\n/**\n * Interface representing the arguments required to override a component.\n *\n * @private\n */\ninterface OverrideArgs<Props> {\n  /** The properties of the component. */\n  props: Props;\n\n  /** The component which should be rendered. */\n  Component: ComponentType<Props>;\n\n  /** The current Slot context. */\n  context: BoxContext<Props>;\n\n  /** The name of the component. */\n  name: string;\n}\n\n/**\n * Represents the result of an override operation.\n *\n * @private\n */\ninterface OverrideResult {\n  /** The properties of the override result. */\n  props: {\n    /** A string indicating the override data. */\n    'data-override'?: string;\n  };\n}\n\nconst triggers: string[] = ['className', 'style'];\n\n/**\n * Overrides the properties of a given context based on certain conditions.\n *\n * @param args.context - The context object.\n * @param args.props - The properties object.\n * @returns The result containing the updated properties or undefined if no overrides are applied.\n * @public\n */\nexport function override<Props extends Record<string, any>>({\n  context,\n  props\n}: OverrideArgs<Props>): OverrideResult | undefined {\n  const causes: string[] = [];\n  const { namespace, assigned, override } = context.slots;\n  const slot: Record<string, any> | undefined = assigned[namespace.join('.')];\n\n  if (typeof props['data-override'] === 'string') {\n    causes.push(...props['data-override'].split(' '));\n  }\n\n  if (override && !causes.includes('context')) causes.push('context');\n  if ('className' in props && !causes.includes('className')) causes.push('className');\n\n  //\n  // For style we need to take a more sophisticated approach, users are allowed\n  // to define CSS variables in the style prop, so we need to check if the keys\n  // are prefixed with `--` or not.\n  //\n  if ('style' in props && !causes.includes('style')) {\n    const style = props.style as CSSProperties;\n    const keys = Object.keys(style);\n\n    if (keys.some((key) => !isCSSVariable(key))) {\n      causes.push('style');\n    }\n  }\n\n  if (slot) {\n    Object.keys(slot).forEach(function forEach(name) {\n      if (triggers.includes(name) && !causes.includes(name)) causes.push(name);\n      if (!causes.includes('slot')) causes.push('slot');\n    });\n  }\n\n  if (!causes.length) return;\n  return {\n    props: useDataAttributes({ override: causes })\n  };\n}\n","import { useDataAttributes } from '@bento/use-data-attributes';\nimport { type BoxContext } from '@bento/box';\nimport { type ComponentType } from 'react';\n\n/**\n * Interface representing the arguments required for replacing a component.\n *\n * @interface ReplaceArgs\n * @property {Props} props - The properties of the component.\n * @property {ComponentType<Props>} Component - The component which should be rendered.\n * @property {SlotContext<Props>} context - The current Slot context.\n * @property {string} name - The name of the component.\n * @private\n */\ninterface ReplaceArgs<Props> {\n  props: Props;\n  Component: ComponentType<Props>;\n  context: BoxContext<Props>;\n  name: string;\n}\n\n/**\n * Interface for props override objects\n * @private\n */\ninterface PropsOverride {\n  /** The properties of the props override. */\n  props: Record<string, any>;\n}\n\n/**\n * Checks if the provided data is a props override instead of a component\n * override.\n *\n * @param data - The data to check.\n * @returns Returns true if the data is a props override, false otherwise.\n * @private\n */\nfunction isPropsOverride(data: any): data is PropsOverride {\n  if (typeof data !== 'object' || Array.isArray(data)) return false;\n\n  const keys = Object.keys(data);\n  return keys.includes('props') && keys.length === 1;\n}\n\n/**\n * Replaces a component in the context with the specified name and props.\n *\n * @param args.props - The properties to be passed to the component.\n * @param args.name - The name of the component to be replaced.\n * @param args.context - The context containing the components.\n * @returns An object containing the updated props, the component to be replaced, and the updated context.\n * @public\n */\nexport function replace<Props extends Record<string, any>>({ props, name, context }: ReplaceArgs<Props>) {\n  if (!context.env || !context.env.components || !(name in context.env.components)) return;\n\n  const target = context.env.components[name];\n  if (!target) return;\n\n  const result = {\n    context: {\n      slots: {\n        override: !!target\n      }\n    },\n    props: {}\n  } as any;\n\n  const causes = (props['data-override'] || '').split(' ').filter(Boolean);\n  if (!causes.includes('context')) causes.push('context');\n  result.props = useDataAttributes({ override: causes });\n\n  if (isPropsOverride(target)) result.props = { ...target.props };\n  else result.Component = target;\n\n  return result;\n}\n","/// <reference types=\"vite/client\" />\nimport React, { useContext, memo } from 'react';\nimport { mergeProps } from 'react-aria';\nimport { useDataAttributes } from '@bento/use-data-attributes';\nimport { useDeepCompareMemo } from 'use-deep-compare';\nimport { Box, type BoxContext } from '@bento/box';\nimport { BentoError } from '@bento/error';\nimport { withForwardRef } from '@bento/forward';\nimport { override } from './override.ts';\nimport { replace } from './replace.ts';\n\n/**\n * Interface representing a collection of slots.\n */\nexport interface Slots {\n  /**\n   * A named part of a component that can be customized. This is implemented by the consuming component.\n   * The exposed slot names of a component are available in the components documentation.\n   */\n  slot?: string;\n  /**\n   * An object that contains the customizations for the slots.\n   * The main way you interact with the slot system as a consumer.\n   */\n  slots?: Record<string, object | Function>;\n}\n\n//\n// A list of libraries that are currently registered by the system. This is\n// needed to prevent duplicate component names from being registered as the\n// names are used to target the correct component with slot overrides.\n//\nexport const library = new Set<string>();\n\n/**\n * Higher-order component that wraps a given component with slot functionality.\n * This allows components to be dynamically replaced or overridden based on the provided context and slots.\n *\n * @param name - The unique name of the component. This is used to identify the component for slot overrides.\n * @param Component - The component that should be rendered.\n * @param modifiers - The modifier functions that should be applied to the component.\n * @returns The wrapped component.\n * @public\n *\n * @throws {BentoError} If the component name has already been registered (in development mode).\n * @throws {BentoError} If any of the supplied modifiers are not functions.\n *\n * @example\n * ```tsx\n * const MyComponent = () => <div>My Component</div>;\n * const SlottedMyComponent = withSlots('MyComponent', MyComponent);\n * ```\n */\nexport function withSlots<Props extends object>(\n  name: string,\n  Component: React.ComponentType<Props>,\n  modifiers = [replace, override]\n) {\n  //\n  // Use @bento/forward to handle ref forwarding for React 18/19 compatibility\n  // and to ensure the component is wrapped with forwardRef if needed.\n  //\n  const ComponentWithRef = withForwardRef(Component);\n  const SlottedForwardRef = withForwardRef(function SlottedComponent(propsAndSlots: Props & Slots, forwardedRef: any) {\n    const { slot = '', slots = {}, ...restProps } = propsAndSlots;\n    const dataAttrs = useDataAttributes({ slot });\n    let props = { ...restProps } as Props;\n    let Element: React.ComponentType<any> = ComponentWithRef;\n\n    //\n    // We need to create a new context object to prevent introducing properties\n    // to a context that is shared across components. Without cloning the context\n    // properties like `namespace` will become corrupted as they would start to\n    // include the slot name of siblings instead of just being a pure parent/child\n    // relationship.\n    //\n    let ctx = { ...useContext<BoxContext<Props>>(Box) };\n    ctx.env = { ...ctx.env };\n    ctx.slots = { ...ctx.slots };\n    ctx.slots.namespace = [...ctx.slots.namespace, slot].filter(Boolean);\n\n    const currentNamespace = ctx.slots.namespace.join('.');\n    const inheritedSlots: Record<string, any> = {};\n    const prefix = `${currentNamespace}.`;\n\n    //\n    // Only inherit slots from the parent that match the current namespace.\n    // If the current namespace is empty (root level), inherit all slots\n    // that don't contain a dot (i.e., slots at the root level only).\n    //\n    for (const key in ctx.slots.assigned) {\n      if (currentNamespace === '' && !key.includes('.')) {\n        inheritedSlots[key] = ctx.slots.assigned[key];\n      } else if (key === currentNamespace || key.startsWith(prefix)) {\n        inheritedSlots[key] = ctx.slots.assigned[key];\n      }\n    }\n\n    ctx.slots.assigned = inheritedSlots;\n\n    //\n    // merge the new slots with the assigned slots,\n    // parent component slots should take precedence over child ones.\n    //\n    for (const slotKey in slots) {\n      // Build the fully qualified slot key by prefixing with current namespace\n      const namespacedKey = ctx.slots.namespace.length > 0 ? `${currentNamespace}.${slotKey}` : slotKey;\n      const assignedSlot = ctx.slots.assigned[namespacedKey];\n      const newSlot = slots[slotKey];\n\n      //\n      // New slots are assigned if none exist, or merged with the new slots.\n      // If the assigned slot exists and is an object, keep it as is.\n      //\n      if (!assignedSlot) {\n        ctx.slots.assigned[namespacedKey] = newSlot;\n      } else if (typeof assignedSlot === 'object') {\n        ctx.slots.assigned[namespacedKey] = { ...newSlot, ...assignedSlot };\n      } else if (typeof assignedSlot === 'function') {\n        const existingPrevious = assignedSlot.__slotPrevious || [];\n        const newPrevious = [newSlot, ...existingPrevious];\n        const mergedFnSlot = function mergedFnSlot({ props, original, previous }: any) {\n          return assignedSlot({ props, original, previous: previous || newPrevious });\n        };\n\n        mergedFnSlot.__slotPrevious = newPrevious;\n        ctx.slots.assigned[namespacedKey] = mergedFnSlot;\n      }\n    }\n\n    //\n    // Modifiers allow you to manipulate the context, props, and component with\n    // a single function. This makes it easier to turn on or off functionality\n    // that you might not need for your components.\n    //\n    // For Bento based components we supply the following modifiers by default:\n    // - replace: Allows you to replace the component with another component\n    //   based on the slots and provided context.\n    // - override: Makes a note of potential design or functional overrides\n    //   that are applied to the component. Making it easier to provide support\n    //   to find common overrides in your application.\n    //\n    modifiers.forEach(function forEach(modifier) {\n      const mods: {\n        Component?: React.ComponentType<Props>;\n        context?: Partial<BoxContext<Props>> | Record<string, any>;\n        props?: object;\n      } =\n        modifier({\n          Component: Element,\n          context: ctx,\n          props,\n          name\n        }) || {};\n\n      if (typeof mods.context === 'object')\n        ctx = {\n          env: { ...ctx.env, ...mods.context?.env },\n          slots: { ...ctx.slots, ...mods.context?.slots }\n        };\n\n      if (typeof mods.props === 'object') props = { ...props, ...mods.props };\n      if (mods.Component) Element = mods.Component as React.ComponentType<any>;\n    });\n\n    const baseProps = { ...(props as object) } as Props;\n\n    // Add ref to props. withForwardRef handles the React 18/19 differences:\n    // - React 18: Component is wrapped with forwardRef, so ref is passed separately\n    // - React 19: ref is just a regular prop\n    const propsWithRef = forwardedRef != null ? ({ ...baseProps, ref: forwardedRef } as Props) : baseProps;\n    const mergedProps = mergeProps(propsWithRef, dataAttrs) as Props;\n\n    const context = useDeepCompareMemo(() => ctx, [ctx]);\n    const rendered = (\n      <Box.Provider value={context}>\n        <Element {...mergedProps} />\n      </Box.Provider>\n    );\n\n    const slotted = ctx.slots.assigned[ctx.slots.namespace.join('.')];\n\n    if (typeof slotted !== 'function') return rendered;\n    return slotted({ props: mergedProps, original: rendered.props.children });\n  });\n\n  const SlottedComponent = memo(SlottedForwardRef);\n  SlottedComponent.displayName = `Slotted(${name})`;\n\n  //\n  // Mark this as a Bento component wrapped with withSlots\n  // This is used by the contains() utility to identify valid slotted components\n  //\n  (SlottedComponent as any).bento = true;\n\n  if (process.env.NODE_ENV !== 'production') {\n    //\n    // This enables re-render tracking for every Bento based component using the\n    // `@welldone-software/why-did-you-render` package.\n    //\n    (SlottedComponent as any).whyDidYouRender = true;\n\n    //\n    // We want to throw the following error only in a development environment.\n    //\n    // While the requirement is to have unique components so each component can\n    // be individually and correctly targeted using our provided context we\n    // want to be mindful that we're breaking an application for the right\n    // reasons. We should only throw in production if an application cannot\n    // recover from this error.\n    //\n    // In the case of these overrides\n    //\n    if (library.has(name) && !import.meta.hot)\n      throw new BentoError({\n        message: 'The supplied component %s has already been registered.',\n        args: [name],\n        method: 'withSlots',\n        name: 'slots'\n      });\n\n    library.add(name);\n  }\n\n  modifiers.forEach(function forEach(modifier) {\n    if (typeof modifier !== 'function')\n      throw new BentoError({\n        message: 'The supplied component modifier is not a function.',\n        method: 'withSlots',\n        name: 'slots'\n      });\n  });\n\n  return SlottedComponent;\n}\n","import React from 'react';\n\n/**\n * Checks if children contain all required slot assignments, including namespaced paths.\n *\n * This utility iterates through React children to verify that all required\n * slot names are present. It supports both simple slot names ('trigger') and\n * namespaced paths ('submit.icon') for deeply nested slot validation.\n *\n * **Important:** Only components wrapped with `withSlots()` are validated. Raw HTML\n * elements with `slot` props are ignored, as they are not part of the Bento slot system.\n *\n * @param slotNames - Array of required slot names to check for (e.g., ['trigger', 'submit.icon'])\n * @param children - React children to search through\n * @returns true if all required slots are found, false otherwise. Returns true\n *          for render prop functions since slots can't be validated until executed.\n *\n * @example\n * ```tsx\n * // Simple slot validation\n * function MyComponent({ children }) {\n *   if (!contains(['trigger', 'content'], children)) {\n *     throw new BentoError({\n *       name: 'my-component',\n *       method: 'MyComponent',\n *       message: 'Missing required slots: trigger and content'\n *     });\n *   }\n *   return <div>{children}</div>;\n * }\n *\n * // Namespaced slot validation\n * function Form({ children }) {\n *   if (!contains(['submit.icon'], children)) {\n *     throw new Error('Submit button must have an icon');\n *   }\n *   return <form>{children}</form>;\n * }\n * ```\n *\n * @public\n */\nexport function contains(slotNames: string[], children: React.ReactNode): boolean {\n  if (!slotNames || slotNames.length === 0) return true;\n  if (!children) return false;\n\n  // Skip validation for render prop functions - we can't inspect slots\n  // until the function is executed with state/props\n  if (typeof children === 'function') return true;\n\n  const foundSlots = new Set<string>();\n\n  /**\n   * Recursively search through children to find slot assignments, building\n   * the path as we go to support namespaced lookups like 'submit.icon'.\n   *\n   * When a child has a slot prop, we add both the simple slot name and the full\n   * namespaced path to our set. For example, if we find:\n   * - `<Button slot=\"submit\"><Icon slot=\"icon\" /></Button>`\n   *\n   * We'll add: 'submit', 'submit', 'icon', and 'submit.icon' to foundSlots.\n   *\n   * Children without slot props (like wrapper divs) are searched through\n   * transparently without extending the path.\n   *\n   * Only components wrapped with `withSlots()` are considered valid slotted\n   * components. They can be identified by the `bento` property set to `true`.\n   *\n   * @param node - The React children to search through\n   * @param path - The accumulated path of slot names from parent to current level\n   */\n  function search(node: React.ReactNode, path: string[] = []): void {\n    React.Children.forEach(node, function processChild(child) {\n      if (!React.isValidElement(child)) return;\n\n      // Check if this is a component wrapped with withSlots\n      // withSlots components have a `bento` property set to true\n      const childType = child.type as any;\n      const isSlottedComponent = childType?.bento === true;\n\n      // Check if this child has a slot prop\n      const slot = (child.props as any)?.slot;\n      if (typeof slot === 'string' && slot.length > 0) {\n        // Only process slot assignments on withSlots components\n        if (isSlottedComponent) {\n          // Build the full path including this slot\n          const fullPath = [...path, slot];\n          const pathString = fullPath.join('.');\n\n          // Add both the simple slot name and the full namespaced path\n          foundSlots.add(slot);\n          foundSlots.add(pathString);\n\n          // Continue searching children with the updated path\n          if (child.props?.children) {\n            search(child.props.children, fullPath);\n          }\n        } else {\n          // Not a withSlots component - search children without extending path\n          if (child.props?.children) {\n            search(child.props.children, path);\n          }\n        }\n      } else {\n        // No slot on this child, but continue searching its children\n        // without extending the path\n        if (child.props?.children) {\n          search(child.props.children, path);\n        }\n      }\n    });\n  }\n\n  search(children);\n\n  // Check if all required slots were found\n  return slotNames.every((name) => foundSlots.has(name));\n}\n"]}