@vitus-labs/attrs 1.2.1 → 1.2.3-alpha.56
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.
- package/LICENSE +1 -1
- package/README.md +175 -0
- package/lib/analysis/index.js.html +1 -1
- package/lib/index.d.ts +406 -324
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +187 -197
- package/lib/index.js.map +1 -1
- package/package.json +20 -25
- package/lib/types/attrs.d.ts +0 -4
- package/lib/types/attrs.d.ts.map +0 -1
- package/lib/types/hoc/attrsHoc.d.ts +0 -6
- package/lib/types/hoc/attrsHoc.d.ts.map +0 -1
- package/lib/types/hoc/index.d.ts +0 -3
- package/lib/types/hoc/index.d.ts.map +0 -1
- package/lib/types/hooks/index.d.ts +0 -3
- package/lib/types/hooks/index.d.ts.map +0 -1
- package/lib/types/hooks/useRef.d.ts +0 -8
- package/lib/types/hooks/useRef.d.ts.map +0 -1
- package/lib/types/index.d.ts +0 -13
- package/lib/types/index.d.ts.map +0 -1
- package/lib/types/init.d.ts +0 -9
- package/lib/types/init.d.ts.map +0 -1
- package/lib/types/isAttrsComponent.d.ts +0 -4
- package/lib/types/isAttrsComponent.d.ts.map +0 -1
- package/lib/types/types/AttrsComponent.d.ts +0 -276
- package/lib/types/types/AttrsComponent.d.ts.map +0 -1
- package/lib/types/types/InitAttrsComponent.d.ts +0 -6
- package/lib/types/types/InitAttrsComponent.d.ts.map +0 -1
- package/lib/types/types/attrs.d.ts +0 -2
- package/lib/types/types/attrs.d.ts.map +0 -1
- package/lib/types/types/config.d.ts +0 -10
- package/lib/types/types/config.d.ts.map +0 -1
- package/lib/types/types/configuration.d.ts +0 -23
- package/lib/types/types/configuration.d.ts.map +0 -1
- package/lib/types/types/hoc.d.ts +0 -4
- package/lib/types/types/hoc.d.ts.map +0 -1
- package/lib/types/types/utils.d.ts +0 -18
- package/lib/types/types/utils.d.ts.map +0 -1
- package/lib/types/utils/attrs.d.ts +0 -9
- package/lib/types/utils/attrs.d.ts.map +0 -1
- package/lib/types/utils/chaining.d.ts +0 -6
- package/lib/types/utils/chaining.d.ts.map +0 -1
- package/lib/types/utils/collection.d.ts +0 -4
- package/lib/types/utils/collection.d.ts.map +0 -1
- package/lib/types/utils/compose.d.ts +0 -4
- package/lib/types/utils/compose.d.ts.map +0 -1
- package/lib/types/utils/statics.d.ts +0 -7
- package/lib/types/utils/statics.d.ts.map +0 -1
package/LICENSE
CHANGED
package/README.md
CHANGED
|
@@ -1 +1,176 @@
|
|
|
1
1
|
# @vitus-labs/attrs
|
|
2
|
+
|
|
3
|
+
Immutable, chainable default-props factory for React components.
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/@vitus-labs/attrs)
|
|
6
|
+
[](https://github.com/vitus-labs/ui-system/blob/main/LICENSE)
|
|
7
|
+
|
|
8
|
+
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.
|
|
9
|
+
|
|
10
|
+
## Features
|
|
11
|
+
|
|
12
|
+
- **Immutable chaining** — every method returns a new component, never mutates the original
|
|
13
|
+
- **Props merge order** — `priorityAttrs` > `attrs` > explicit props, with full control over precedence
|
|
14
|
+
- **Prop filtering** — strip internal props before they reach the DOM
|
|
15
|
+
- **HOC composition** — named HOCs via `.compose()` with selective removal
|
|
16
|
+
- **Static metadata** — attach and access custom data via `.statics()` / `.meta`
|
|
17
|
+
- **TypeScript inference** — generics accumulate through the chain
|
|
18
|
+
|
|
19
|
+
## Installation
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
npm install @vitus-labs/attrs
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## Quick Start
|
|
26
|
+
|
|
27
|
+
```tsx
|
|
28
|
+
import attrs from '@vitus-labs/attrs'
|
|
29
|
+
import { Element } from '@vitus-labs/elements'
|
|
30
|
+
|
|
31
|
+
const Button = attrs({ name: 'Button', component: Element })
|
|
32
|
+
.attrs({ tag: 'button', alignX: 'center', alignY: 'center' })
|
|
33
|
+
|
|
34
|
+
// Renders Element with tag="button", alignX="center", alignY="center"
|
|
35
|
+
<Button label="Click me" />
|
|
36
|
+
|
|
37
|
+
// Explicit props override attrs defaults
|
|
38
|
+
<Button tag="a" label="Link button" />
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## API
|
|
42
|
+
|
|
43
|
+
### attrs(options)
|
|
44
|
+
|
|
45
|
+
Creates an attrs-enhanced component.
|
|
46
|
+
|
|
47
|
+
```tsx
|
|
48
|
+
const Component = attrs({
|
|
49
|
+
name: 'ComponentName', // required — sets displayName
|
|
50
|
+
component: BaseComponent, // required — the React component to wrap
|
|
51
|
+
})
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
### .attrs(props | callback, options?)
|
|
55
|
+
|
|
56
|
+
Add default props. Can be called multiple times — defaults stack left-to-right.
|
|
57
|
+
|
|
58
|
+
```tsx
|
|
59
|
+
// Object form — static defaults
|
|
60
|
+
Button.attrs({ tag: 'button' })
|
|
61
|
+
|
|
62
|
+
// Callback form — computed defaults based on current props
|
|
63
|
+
Button.attrs((props) => ({
|
|
64
|
+
'aria-label': props.label,
|
|
65
|
+
}))
|
|
66
|
+
|
|
67
|
+
// Priority — resolved before regular attrs, cannot be overridden by explicit props
|
|
68
|
+
Button.attrs({ tag: 'button' }, { priority: true })
|
|
69
|
+
|
|
70
|
+
// Filter — remove props before passing to the underlying component
|
|
71
|
+
Button.attrs({}, { filter: ['internalFlag', 'variant'] })
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
**Props merge order:**
|
|
75
|
+
|
|
76
|
+
```text
|
|
77
|
+
priorityAttrs (highest) → attrs → explicit props (lowest for priority, highest for regular)
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
For regular attrs, explicit props win. For priority attrs, the priority value wins.
|
|
81
|
+
|
|
82
|
+
### .config(options)
|
|
83
|
+
|
|
84
|
+
Reconfigure the component. Returns a new component instance.
|
|
85
|
+
|
|
86
|
+
```tsx
|
|
87
|
+
// Rename
|
|
88
|
+
Button.config({ name: 'PrimaryButton' })
|
|
89
|
+
|
|
90
|
+
// Swap the base component
|
|
91
|
+
Button.config({ component: AnotherComponent })
|
|
92
|
+
|
|
93
|
+
// Enable debug mode — adds data-attrs attribute in development
|
|
94
|
+
Button.config({ DEBUG: true })
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
### .compose(hocs)
|
|
98
|
+
|
|
99
|
+
Attach named Higher-Order Components. Applied in declaration order.
|
|
100
|
+
|
|
101
|
+
```tsx
|
|
102
|
+
const Enhanced = Button.compose({
|
|
103
|
+
withTheme: (Component) => (props) => (
|
|
104
|
+
<ThemeProvider><Component {...props} /></ThemeProvider>
|
|
105
|
+
),
|
|
106
|
+
withTracking: trackingHoc,
|
|
107
|
+
})
|
|
108
|
+
|
|
109
|
+
// Remove a specific HOC from the chain
|
|
110
|
+
const WithoutTracking = Enhanced.compose({ withTracking: null })
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
### .statics(metadata)
|
|
114
|
+
|
|
115
|
+
Attach metadata accessible via the `.meta` property.
|
|
116
|
+
|
|
117
|
+
```tsx
|
|
118
|
+
const Button = attrs({ name: 'Button', component: Element })
|
|
119
|
+
.statics({ category: 'action', sizes: ['sm', 'md', 'lg'] })
|
|
120
|
+
|
|
121
|
+
Button.meta.category // => 'action'
|
|
122
|
+
Button.meta.sizes // => ['sm', 'md', 'lg']
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
### isAttrsComponent(value)
|
|
126
|
+
|
|
127
|
+
Runtime type guard.
|
|
128
|
+
|
|
129
|
+
```tsx
|
|
130
|
+
import { isAttrsComponent } from '@vitus-labs/attrs'
|
|
131
|
+
|
|
132
|
+
isAttrsComponent(Button) // => true
|
|
133
|
+
isAttrsComponent('div') // => false
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
### getDefaultAttrs()
|
|
137
|
+
|
|
138
|
+
Retrieve the computed default props for a component.
|
|
139
|
+
|
|
140
|
+
```tsx
|
|
141
|
+
Button.getDefaultAttrs() // => { tag: 'button', alignX: 'center', ... }
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
## TypeScript
|
|
145
|
+
|
|
146
|
+
Each `.attrs<P>()` call adds `P` to the component's prop types through `MergeTypes`:
|
|
147
|
+
|
|
148
|
+
```tsx
|
|
149
|
+
const Base = attrs({ name: 'Base', component: Element })
|
|
150
|
+
|
|
151
|
+
const Typed = Base
|
|
152
|
+
.attrs<{ variant: 'primary' | 'secondary' }>({ variant: 'primary' })
|
|
153
|
+
.attrs<{ size?: 'sm' | 'md' | 'lg' }>({})
|
|
154
|
+
|
|
155
|
+
// Typed accepts: Element props + { variant, size? }
|
|
156
|
+
<Typed variant="secondary" size="lg" label="Hello" />
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
Access the accumulated types via type-only properties:
|
|
160
|
+
|
|
161
|
+
```tsx
|
|
162
|
+
type AllProps = typeof Typed.$$types
|
|
163
|
+
type OriginalProps = typeof Typed.$$originTypes
|
|
164
|
+
type ExtendedProps = typeof Typed.$$extendedTypes
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
## Peer Dependencies
|
|
168
|
+
|
|
169
|
+
| Package | Version |
|
|
170
|
+
| ------- | ------- |
|
|
171
|
+
| react | >= 19 |
|
|
172
|
+
| @vitus-labs/core | * |
|
|
173
|
+
|
|
174
|
+
## License
|
|
175
|
+
|
|
176
|
+
MIT
|
|
@@ -5386,7 +5386,7 @@ var drawChart = (function (exports) {
|
|
|
5386
5386
|
</script>
|
|
5387
5387
|
<script>
|
|
5388
5388
|
/*<!--*/
|
|
5389
|
-
const data = {"version":2,"tree":{"name":"root","children":[{"name":"index.js","children":[{"name":"src","children":[{"name":"
|
|
5389
|
+
const data = {"version":2,"tree":{"name":"root","children":[{"name":"index.js","children":[{"name":"src","children":[{"name":"utils","children":[{"uid":"4bb57168-1","name":"attrs.ts"},{"uid":"4bb57168-11","name":"chaining.ts"},{"uid":"4bb57168-13","name":"compose.ts"},{"uid":"4bb57168-15","name":"statics.ts"}]},{"name":"hoc","children":[{"uid":"4bb57168-3","name":"attrsHoc.tsx"},{"uid":"4bb57168-5","name":"index.ts"}]},{"name":"hooks","children":[{"uid":"4bb57168-7","name":"useRef.ts"},{"uid":"4bb57168-9","name":"index.ts"}]},{"uid":"4bb57168-17","name":"attrs.tsx"},{"uid":"4bb57168-19","name":"init.ts"},{"uid":"4bb57168-21","name":"isAttrsComponent.tsx"},{"uid":"4bb57168-23","name":"index.ts"}]}]}],"isRoot":true},"nodeParts":{"4bb57168-1":{"renderedLength":440,"gzipLength":269,"brotliLength":0,"metaUid":"4bb57168-0"},"4bb57168-3":{"renderedLength":1263,"gzipLength":594,"brotliLength":0,"metaUid":"4bb57168-2"},"4bb57168-5":{"renderedLength":0,"gzipLength":0,"brotliLength":0,"metaUid":"4bb57168-4"},"4bb57168-7":{"renderedLength":702,"gzipLength":384,"brotliLength":0,"metaUid":"4bb57168-6"},"4bb57168-9":{"renderedLength":0,"gzipLength":0,"brotliLength":0,"metaUid":"4bb57168-8"},"4bb57168-11":{"renderedLength":262,"gzipLength":182,"brotliLength":0,"metaUid":"4bb57168-10"},"4bb57168-13":{"renderedLength":168,"gzipLength":152,"brotliLength":0,"metaUid":"4bb57168-12"},"4bb57168-15":{"renderedLength":163,"gzipLength":144,"brotliLength":0,"metaUid":"4bb57168-14"},"4bb57168-17":{"renderedLength":3086,"gzipLength":1186,"brotliLength":0,"metaUid":"4bb57168-16"},"4bb57168-19":{"renderedLength":496,"gzipLength":291,"brotliLength":0,"metaUid":"4bb57168-18"},"4bb57168-21":{"renderedLength":310,"gzipLength":230,"brotliLength":0,"metaUid":"4bb57168-20"},"4bb57168-23":{"renderedLength":61,"gzipLength":75,"brotliLength":0,"metaUid":"4bb57168-22"}},"nodeMetas":{"4bb57168-0":{"id":"/src/utils/attrs.ts","moduleParts":{"index.js":"4bb57168-1"},"imported":[{"uid":"4bb57168-24"}],"importedBy":[{"uid":"4bb57168-16"},{"uid":"4bb57168-2"}]},"4bb57168-2":{"id":"/src/hoc/attrsHoc.tsx","moduleParts":{"index.js":"4bb57168-3"},"imported":[{"uid":"4bb57168-25"},{"uid":"4bb57168-0"},{"uid":"4bb57168-26"}],"importedBy":[{"uid":"4bb57168-4"}]},"4bb57168-4":{"id":"/src/hoc/index.ts","moduleParts":{"index.js":"4bb57168-5"},"imported":[{"uid":"4bb57168-2"}],"importedBy":[{"uid":"4bb57168-16"}]},"4bb57168-6":{"id":"/src/hooks/useRef.ts","moduleParts":{"index.js":"4bb57168-7"},"imported":[{"uid":"4bb57168-25"}],"importedBy":[{"uid":"4bb57168-8"}]},"4bb57168-8":{"id":"/src/hooks/index.ts","moduleParts":{"index.js":"4bb57168-9"},"imported":[{"uid":"4bb57168-6"}],"importedBy":[{"uid":"4bb57168-16"}]},"4bb57168-10":{"id":"/src/utils/chaining.ts","moduleParts":{"index.js":"4bb57168-11"},"imported":[],"importedBy":[{"uid":"4bb57168-16"}]},"4bb57168-12":{"id":"/src/utils/compose.ts","moduleParts":{"index.js":"4bb57168-13"},"imported":[],"importedBy":[{"uid":"4bb57168-16"}]},"4bb57168-14":{"id":"/src/utils/statics.ts","moduleParts":{"index.js":"4bb57168-15"},"imported":[{"uid":"4bb57168-24"}],"importedBy":[{"uid":"4bb57168-16"}]},"4bb57168-16":{"id":"/src/attrs.tsx","moduleParts":{"index.js":"4bb57168-17"},"imported":[{"uid":"4bb57168-24"},{"uid":"4bb57168-25"},{"uid":"4bb57168-4"},{"uid":"4bb57168-8"},{"uid":"4bb57168-0"},{"uid":"4bb57168-10"},{"uid":"4bb57168-12"},{"uid":"4bb57168-14"},{"uid":"4bb57168-26"}],"importedBy":[{"uid":"4bb57168-18"}]},"4bb57168-18":{"id":"/src/init.ts","moduleParts":{"index.js":"4bb57168-19"},"imported":[{"uid":"4bb57168-24"},{"uid":"4bb57168-16"}],"importedBy":[{"uid":"4bb57168-22"}]},"4bb57168-20":{"id":"/src/isAttrsComponent.tsx","moduleParts":{"index.js":"4bb57168-21"},"imported":[],"importedBy":[{"uid":"4bb57168-22"}]},"4bb57168-22":{"id":"/src/index.ts","moduleParts":{"index.js":"4bb57168-23"},"imported":[{"uid":"4bb57168-18"},{"uid":"4bb57168-20"}],"importedBy":[],"isEntry":true},"4bb57168-24":{"id":"@vitus-labs/core","moduleParts":{},"imported":[],"importedBy":[{"uid":"4bb57168-18"},{"uid":"4bb57168-16"},{"uid":"4bb57168-0"},{"uid":"4bb57168-14"}]},"4bb57168-25":{"id":"react","moduleParts":{},"imported":[],"importedBy":[{"uid":"4bb57168-16"},{"uid":"4bb57168-2"},{"uid":"4bb57168-6"}]},"4bb57168-26":{"id":"react/jsx-runtime","moduleParts":{},"imported":[],"importedBy":[{"uid":"4bb57168-16"},{"uid":"4bb57168-2"}]}},"env":{"rollup":"4.23.0"},"options":{"gzip":true,"brotli":false,"sourcemap":false}};
|
|
5390
5390
|
|
|
5391
5391
|
const run = () => {
|
|
5392
5392
|
const width = window.innerWidth;
|