22
33A very simple way to attach javascript to the DOM. When even [ petite-vue] ( https://github.com/vuejs/petite-vue ) or [ alpine.js] ( https://github.com/alpinejs/alpine/ ) would be too much.
44
5- 💾 less than 0.5kb (minify and gzip)
5+ 💾 ~ 0.6kb (minify and gzip)
66
77## Installation
88
@@ -15,19 +15,27 @@ npm i @very-simple/components
1515``` js
1616// components/gallery.js
1717
18- import { registerComponent } from ' @very-simple/components'
18+ import { registerComponent , defineProps } from ' @very-simple/components'
1919
20+ const props = defineProps ({ loop: Boolean })
2021registerComponent (' gallery' , ({ el, ref, refs }) => {
22+ // Props are read from el's dataset and automatically converted to the correct
23+ // type. Default values are also possible, see documentation.
24+ const { loop } = props (el)
25+
2126 // Multiple HTML elements can have the same `ref` name. They will be
2227 // grouped in `refs` ...
2328 const { slides } = refs
29+
2430 // ... whereas `ref` only stores a single element per name.
2531 const { prev , next } = ref
2632
2733 let currentIndex = 0
2834 const maxIndex = slides .length - 1
2935
3036 const selectSlide = index => {
37+ if (! loop && (index < 0 || index > maxIndex)) return
38+
3139 currentIndex = index < 0 ? maxIndex : index > maxIndex ? 0 : index
3240 slides .forEach ((el , index ) => (el .hidden = index !== currentIndex))
3341 }
@@ -44,7 +52,7 @@ registerComponent('gallery', ({ el, ref, refs }) => {
4452``` html
4553<!-- index.html -->
4654
47- <div id =" my-gallery" data-simple-component =" gallery" >
55+ <div id =" my-gallery" data-simple-component =" gallery" data-loop = " true " >
4856 <div data-ref =" slides" >A</div >
4957 <div data-ref =" slides" >B</div >
5058 <div data-ref =" slides" >C</div >
@@ -92,6 +100,44 @@ mountComponent(el: HTMLElement)
92100mountComponent (root ? : HTMLElement )
93101` ` `
94102
103+ ### Define Props
104+
105+ Define properties to automatically convert ` el .dataset ` properties to the
106+ correct type and enable autocompletion.
107+
108+ ` ` ` ts
109+ // You can either define a prop's type by proving a constructor ...
110+ defineProps ({ answer: Number , enabled: Boolean })
111+
112+ // ... or by providing a default value.
113+ defineProps ({ answer: 42 , enabled: true })
114+
115+ // For objects and arrays the default value can be wrapped inside a function
116+ defineProps ({ list : () => [1 , 2 , 3 ] })
117+ ```
118+
119+ Example:
120+
121+ ``` ts
122+ const props = defineProps ({
123+ enabled: true ,
124+ message: String ,
125+ tags : () => [' default' ]
126+ })
127+
128+ export default registerComponent (' my-component' , ({ el }) => {
129+ const { enabled, message, tags } = props (el )
130+ })
131+ ```
132+
133+ ``` html
134+ <div
135+ data-simple-component =" my-component"
136+ data-message =" Hello"
137+ data-tags =' [ "very", "simple", "components" ]'
138+ ></div >
139+ ```
140+
95141### Ignore Elements
96142
97143Sometimes it is useful to skip big DOM elements when searching for components
0 commit comments