<!-- LOGO:BEGIN -->
<div align="center">
  <a href="https://github.com/vitus-labs/ui-system">
    <picture>
      <source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/vitus-labs/ui-system/main/.github/assets/vitus-labs-mark-dark.svg">
      <img alt="vitus·labs" src="https://raw.githubusercontent.com/vitus-labs/ui-system/main/.github/assets/vitus-labs-mark-light.svg" height="64">
    </picture>
  </a>
  &nbsp;&nbsp;&nbsp;&nbsp;
  <picture>
    <source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/vitus-labs/ui-system/main/packages/attrs/assets/logo-dark.svg">
    <img alt="@vitus-labs/attrs" src="https://raw.githubusercontent.com/vitus-labs/ui-system/main/packages/attrs/assets/logo-light.svg" height="64">
  </picture>
</div>
<!-- LOGO:END -->

# @vitus-labs/attrs

Immutable, chainable default-props factory for React components.

[![npm](https://img.shields.io/npm/v/@vitus-labs/attrs)](https://www.npmjs.com/package/@vitus-labs/attrs)
[![license](https://img.shields.io/npm/l/@vitus-labs/attrs)](https://github.com/vitus-labs/ui-system/blob/main/LICENSE)

Think of styled-components' `.attrs()` as a standalone, type-safe composition system. Define default props, swap base components, attach HOCs, and add metadata — all through an immutable chain where every call returns a new component.

## Features

- **Immutable chaining** — every method returns a new component, never mutates the original
- **Props merge order** — `priorityAttrs` → `attrs` → explicit props (last wins), with full control over precedence
- **Prop filtering** — strip internal props before they reach the DOM
- **HOC composition** — named HOCs via `.compose()` with selective removal
- **Static metadata** — attach and access custom data via `.statics()` / `.meta`
- **TypeScript inference** — generics accumulate through the chain

## Installation

```bash
npm install @vitus-labs/attrs
```

## Quick Start

```tsx
import attrs from '@vitus-labs/attrs'
import { Element } from '@vitus-labs/elements'

const Button = attrs({ name: 'Button', component: Element })
  .attrs({ tag: 'button', alignX: 'center', alignY: 'center' })

// Renders Element with tag="button", alignX="center", alignY="center"
<Button label="Click me" />

// Explicit props override attrs defaults
<Button tag="a" label="Link button" />
```

## API

### attrs(options)

Creates an attrs-enhanced component.

```tsx
const Component = attrs({
  name: 'ComponentName',   // required — sets displayName
  component: BaseComponent, // required — the React component to wrap
})
```

### .attrs(props | callback, options?)

Add default props. Can be called multiple times — defaults stack left-to-right.

```tsx
// Object form — static defaults
Button.attrs({ tag: 'button' })

// Callback form — computed defaults based on current props
Button.attrs((props) => ({
  'aria-label': props.label,
}))

// Priority — resolved first, as the lowest layer: regular attrs and explicit props both override it
Button.attrs({ tag: 'button' }, { priority: true })

// Filter — remove props before passing to the underlying component
Button.attrs({}, { filter: ['internalFlag', 'variant'] })
```

**Props merge order:**

```text
priorityAttrs → attrs → explicit props   (last wins)
```

Priority attrs are the lowest layer: they're resolved first, so regular `.attrs()`
callbacks receive them as input (alongside explicit props) and can override them,
and explicit props override both. Use priority attrs for base defaults that later
attrs should be able to compute from.

### .config(options)

Reconfigure the component. Returns a new component instance.

```tsx
// Rename
Button.config({ name: 'PrimaryButton' })

// Swap the base component
Button.config({ component: AnotherComponent })
```

`DEBUG` is accepted by `.config()` but has no runtime effect in attrs: every
non-production build already adds a `data-attrs="<name>"` attribute to the
rendered element, with or without it. (In rocketstyle, `DEBUG: true` does
enable per-render console logging.)

### .compose(hocs)

Attach named Higher-Order Components. The last-declared HOC wraps outermost (renders
first); the first-declared sits closest to the base component. The internal attrs HOC
always stays outside every user HOC, so HOCs see the fully resolved props.

```tsx
const Enhanced = Button.compose({
  withTheme: (Component) => (props) => (
    <ThemeProvider><Component {...props} /></ThemeProvider>
  ),
  withTracking: trackingHoc,
})

// Remove a specific HOC from the chain
const WithoutTracking = Enhanced.compose({ withTracking: null })
```

### .statics(metadata)

Attach metadata accessible via the `.meta` property.

```tsx
const Button = attrs({ name: 'Button', component: Element })
  .statics({ category: 'action', sizes: ['sm', 'md', 'lg'] })

Button.meta.category // => 'action'
Button.meta.sizes    // => ['sm', 'md', 'lg']
```

### isAttrsComponent(value)

Runtime type guard.

```tsx
import { isAttrsComponent } from '@vitus-labs/attrs'

isAttrsComponent(Button) // => true
isAttrsComponent('div')  // => false
```

### getDefaultAttrs(props)

Retrieve the computed default props for a component, given the props the
`.attrs()` callbacks should see (pass `{}` for none).

```tsx
Button.getDefaultAttrs({}) // => { tag: 'button', alignX: 'center', ... }
Button.getDefaultAttrs({ label: 'Save' }) // callbacks receive `label`
```

## TypeScript

Each `.attrs<P>()` call adds `P` to the component's prop types through `MergeTypes`:

```tsx
const Base = attrs({ name: 'Base', component: Element })

const Typed = Base
  .attrs<{ variant: 'primary' | 'secondary' }>({ variant: 'primary' })
  .attrs<{ size?: 'sm' | 'md' | 'lg' }>({})

// Typed accepts: Element props + { variant, size? }
<Typed variant="secondary" size="lg" label="Hello" />
```

Access the accumulated types via type-only properties:

```tsx
type AllProps = typeof Typed.$$types
type OriginalProps = typeof Typed.$$originTypes
type ExtendedProps = typeof Typed.$$extendedTypes
```

## Peer Dependencies

| Package | Version |
| ------- | ------- |
| react | >= 19 |
| @vitus-labs/core | * |

## License

MIT
