posthtml-component 1.1.0 → 2.0.0-beta.1

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.
@@ -1,1043 +1,1043 @@
1
- [![NPM][npm]][npm-url]
2
- [![Coverage][cover]][cover-badge]
3
- [![XO code style][style]][style-url]
4
-
5
- <div align="center">
6
- <img width="300" title="PostHTML" src="http://posthtml.github.io/posthtml/logo.svg">
7
- <h1>PostHTML Components </h1>
8
- <p>A PostHTML plugin for create components with HTML-friendly syntax inspired by Laravel Blade. Slots, stack/push, props, custom tag and much more.</p>
9
- </div>
10
-
11
- ## Installation
12
-
13
- ```bash
14
- npm i -D posthtml-component
15
- ```
16
-
17
- ## Introduction
18
-
19
- This PostHTML plugin provides an HTML-friendly syntax for write components in your templates.
20
- If you are familiar with Blade, React, Vue or similar, you will find familiar syntax as this plugin is inspired by them.
21
- See below a basic example, as code is worth a thousand words.
22
-
23
- **See also the first [PostHTML Bootstrap UI](https://github.com/thewebartisan7/posthtml-bootstrap-ui) using this plugin and check also the [starter template here](https://github.com/thewebartisan7/posthtml-bootstrap-ui-starter).**
24
-
25
- ## Basic example
26
-
27
- Create the component:
28
-
29
- ``` html
30
- <!-- src/button.html -->
31
- <button type="button" class="btn">
32
- <yield></yield>
33
- </button>
34
- ```
35
-
36
- Use the component:
37
-
38
- ``` html
39
- <!-- src/index.html -->
40
- <html>
41
- <body>
42
- <x-button type="submit" class="btn-primary">Submit</x-button>
43
- </body>
44
- </html>
45
- ```
46
-
47
- Init PostHTML:
48
-
49
- ```js
50
- // index.js
51
- const { readFileSync, writeFileSync } = require('fs')
52
-
53
- const posthtml = require('posthtml')
54
- const components = require('posthtml-components')
55
-
56
- posthtml(components({ root: './src' }))
57
- .process(readFileSync('src/index.html', 'utf8'))
58
- .then((result) => writeFileSync('dist/index.html', result.html, 'utf8'))
59
- ```
60
-
61
- Result:
62
-
63
- ``` html
64
- <!-- dist/index.html -->
65
- <html>
66
- <body>
67
- <button type="submit" class="btn btn-primary">Submit</button>
68
- </body>
69
- </html>
70
- ```
71
-
72
- You may notice that the `src/button.html` component has a `type` and `class` attribute, and when we use the component in `src/index.html` we pass `type` and `class` attribute.
73
- The result is that `type` is override, and `class` is merged.
74
-
75
- By default `class` and `style` attributes are merged, while all others attribute are override.
76
- You can also override `class` and `style` attributes by prepending `override:` to the class attribute. Example:
77
-
78
- ```html
79
- <x-button override:class="btn-custom">Submit</x-button>
80
-
81
- <!-- Output -->
82
- <button type="button" class="btn-custom">Submit</button>
83
- ```
84
-
85
- All attributes you pass to the component will be added to the first node of your component or to the node with an attribute names `attributes`,
86
- and only if they are not defined as `props` via `<script props>` or if they are not in the following file
87
- [valid-attributes.js](https://github.com/thewebartisan7/posthtml-components/blob/main/src/valid-attributes.js).
88
- You can also manage valid attributes via options.
89
- More details on this in [Attributes](#attributes) section.
90
-
91
- The `<yield>` tag is where your content will be injected.
92
- In next section you can find all available options and then examples for each feature.
93
-
94
- See also the `docs-src` folder where you can find more examples.
95
- You can run `npm run build` to compile them.
96
-
97
- ## Options
98
-
99
- | Option | Default | Description |
100
- |:------------------------:|:---------------------------------------------:|:------------------------------------------------------------------------------------------------------------------------------|
101
- | **root** | `'./'` | String value as root path for components lookup. |
102
- | **folders** | `['']` | Array of additional multi folders path from `options.root` or any defined namespaces root, fallback or custom. |
103
- | **tagPrefix** | `x-` | String for tag prefix. The plugin will use RegExp with this string. |
104
- | **tag** | `false` | String or boolean value for component tag. Use this with `options.attribute`. Boolean only false. |
105
- | **attribute** | `src` | String value for component attribute for set path. |
106
- | **namespaces** | `[]` | Array of namespace's root path, fallback path and custom path for override. |
107
- | **namespaceSeparator** | `::` | String value for namespace separator to be used with tag name. Example `<x-namespace::button>` |
108
- | **fileExtension** | `html` | String value for file extension of the components used for retrieve x-tag file. |
109
- | **yield** | `yield` | String value for `<yield>` tag name. Where main content of component is injected. |
110
- | **slot** | `slot` | String value for `<slot>` tag name. Used with RegExp by appending `:` (example `<slot:slot-name>`). |
111
- | **fill** | `fill` | String value for `<fill>` tag name. Used with RegExp by appending `:` (example `<fill:slot-name>`). |
112
- | **slotSeparator** | `:` | String value used for separate `<slot>` and `<fill>` tag from their names. |
113
- | **push** | `push` | String value for `<push>` tag name. |
114
- | **stack** | `stack` | String value for `<stack>` tag name. |
115
- | **propsScriptAttribute** | `props` | String value used as attribute in `<script props>` parsed by the plugin to retrieve props of the component. |
116
- | **propsContext** | `props` | String value used as object name inside the script to process process before passed to the component. |
117
- | **propsAttribute** | `props` | String value for props attribute to define props as JSON. |
118
- | **propsSlot** | `props` | String value used to retrieve the props passed to slot via `$slots.slotName.props`. |
119
- | **parserOptions** | `{recognizeSelfClosing: true}` | Object to configure `posthtml-parser`. By default, it enables support for self-closing component tags. |
120
- | **expressions** | `{}` | Object to configure `posthtml-expressions`. You can pre-set locals or customize the delimiters for example. |
121
- | **plugins** | `[]` | PostHTML plugins to apply for every parsed components. |
122
- | **matcher** | `[{tag: options.tagPrefix}]` | Array of object used to match the tags. |
123
- | **attrsParserRules** | `{}` | Additional rules for attributes parser plugin. |
124
- | **strict** | `true` | Boolean value for enable or disable throw an exception. |
125
- | **mergeCustomizer** | `function` | Function callback passed to lodash `mergeWith` for merge `options.expressions.locals` and props passed via attribute `props`. |
126
- | **utilities** | `{merge: _.mergeWith, template: _.template}` | Object of utilities methods to be passed to `<script props>`. By default lodash `mergeWith` and `template`. |
127
- | **elementAttributes** | `{}` | An object with tag name and a function modifier of valid-attributes.js. |
128
- | **safelistAttributes** | `['data-*']` | An array of attributes name to be added to default valid attributes. |
129
- | **blacklistAttributes** | `[]` | An array of attributes name to be removed from default valid attributes. |
130
-
131
- ## Features
132
-
133
- ### Tag names and x-tags
134
-
135
- You can use the components in multiple ways, or also a combination of them.
136
- Like with `posthtml-extend` and `posthtml-modules` you can define a tag name in combination with an attribute name for set the path of the components.
137
-
138
- For example for the same button component `src/button.html` in the basic example we can define the tag name and attribute name and then use it in this way:
139
-
140
- ``` html
141
- <!-- src/index.html -->
142
- <html>
143
- <body>
144
- <component src="button.html">Submit</component>
145
- </body>
146
- </html>
147
- ```
148
-
149
- Init PostHTML:
150
-
151
- ```js
152
- // index.js
153
-
154
- require('posthtml')(require('posthtml-components')({ root: './src', tagName: 'component', attribute: 'src' }))
155
- .process(/* ... */)
156
- .then(/* ... */)
157
- ```
158
-
159
- If you need more control over how to match the tags, you can pass directly an array of matcher or single object via `options.matcher` like shown in below example:
160
-
161
- ```js
162
- // index.js
163
-
164
- const options = {
165
- root: './src',
166
- matcher: [{tag: 'a-tag'}, {tag: 'another-one'}, {tag: new RegExp(`^app-`, 'i')}]
167
- };
168
-
169
- require('posthtml')(require('posthtml-components')(options))
170
- .process(/* ... */)
171
- .then(/* ... */)
172
- ```
173
-
174
- With `posthtml-components` you don't need to specify the path name when you are using `x-tag-name` syntax. See below example.
175
-
176
- Setup PostHTML:
177
-
178
- ```js
179
- // index.js
180
-
181
- const options = {
182
- root: './src',
183
- tagPrefix: 'x-'
184
- };
185
-
186
- require('posthtml')(require('posthtml-components')(options))
187
- .process(/* ... */)
188
- .then(/* ... */)
189
- ```
190
-
191
- Use:
192
-
193
- ``` html
194
- <!-- src/index.html -->
195
- <html>
196
- <body>
197
- <x-button>Submit</x-button>
198
- </body>
199
- </html>
200
- ```
201
-
202
- If your components are in a subfolder then you can use `dot` to access it, example:
203
-
204
- ``` html
205
- <!-- Supposing your button component is located in ./src/components/forms/button.html -->
206
- <x-forms.button>Submit</x-forms.button>
207
- ```
208
-
209
- If your components are in a sub-folder with multiple files, then for avoid typing the main file you can use `index.html` without specify it.
210
- Please see below example to understand better.
211
-
212
- ``` html
213
- <!-- Supposing your modal component is located in ./src/components/modals/index.html -->
214
- <x-modal.index>Submit</x-modal.index>
215
-
216
- <!-- You can omit "index" part since the file is named "index.html" -->
217
- <x-modal>Submit</x-modal>
218
- ```
219
-
220
- #### Parser options
221
-
222
- You may pass options to `posthtml-parser` via `options.parserOptions`.
223
-
224
- ```js
225
- // index.js
226
- const options = {
227
- root: './src',
228
- parserOptions: { decodeEntities: true }
229
- };
230
-
231
- require('posthtml')(require('posthtml-components')(options))
232
- .process('some HTML', options.parserOptions)
233
- .then(/* ... */)
234
- ```
235
-
236
- Important: as you can see, whatever `parserOptions` you pass to the plugin, must also be passed in the `process` method in your code, otherwise your PostHTML build will use `posthtml-parser` defaults and will override anything you've passed to `posthtml-component`.
237
-
238
- #### Self-closing tags
239
-
240
- The plugin supports self-closing tags by default, but you need to make sure to enable them in the `process` method in your code too, by passing `recognizeSelfClosing: true` in the options object:
241
-
242
- ```js
243
- // index.js
244
- require('posthtml')(require('posthtml-components')({root: './src'}))
245
- .process('your HTML...', {recognizeSelfClosing: true})
246
- .then(/* ... */)
247
- ```
248
-
249
- If you don't add this to `process`, PostHTML will use `posthtml-parser` defaults and will not support self-closing component tags. This will result in everything after a self-closing tag not being output.
250
-
251
- ### Multiple folders
252
-
253
- You have full control where to place your components. Once you set the base root path of your components, you can then set multiple folders.
254
- For example let's suppose your main root is `./src` and then you have several folders where you have your components, for example `./src/components` and `./src/layouts`.
255
- You can set up the plugin like below:
256
-
257
- ```js
258
- // index.js
259
- const options = {
260
- root: './src',
261
- folders: ['components', 'layouts']
262
- };
263
-
264
- require('posthtml')(require('posthtml-components')(options))
265
- .process(/* ... */)
266
- .then(/* ... */)
267
- ```
268
-
269
- ### Namespaces
270
-
271
- With namespaces, you can define a top level root path to your components like shown in below example.
272
- It can be useful for handle custom theme, where you define a specific top level root, with fallback root when component it's not found,
273
- and a custom root for override, something like a child theme.
274
-
275
- Thanks to namespace, you can create folders structure like below:
276
-
277
- - `src` (base root folder)
278
- - `components` (folder for components like modal, button, etc.)
279
- - `layouts` (folder for layout components like base layout, header, footer, etc.)
280
- - `theme-dark` (namespace folder for theme-dark)
281
- - `components` (folder for components for theme dark)
282
- - `layouts` (folder for layout components for dark theme)
283
- - `theme-light` (namespace folder for theme-light)
284
- - `components` (folder for components for light theme)
285
- - `layouts` (folder for layout components for dark theme)
286
- - `custom` (custom folder for override your namespace themes)
287
- - `theme-dark` (custom folder for override dark theme)
288
- - `components` (folder for override components of theme dark)
289
- - `layouts` (folder for override layout components of dark theme)
290
- - `theme-light` (custom folder for override light theme)
291
- - `components` (folder for override components of theme dark)
292
- - `layouts` (folder for override layout components of dark theme)
293
-
294
- And the options would be like:
295
-
296
- ```js
297
- // index.js
298
- const options = {
299
- // Main root for component without namespace
300
- root: './src',
301
- // Folders is always appended in 'root' or any defined namespace's folders (base, fallback or custom)
302
- folders: ['components', 'layouts'],
303
- namespaces: [{
304
- // Namespace name will be prepend to tag name (example <x-theme-dark::button>)
305
- name: 'theme-dark',
306
- // Base root of the namespace
307
- root: './src/theme-dark',
308
- // Fallback root when a component it's not found in namespace
309
- fallback: './src',
310
- // Custom root for override, the lookup happen first here
311
- custom: './src/custom/theme-dark'
312
- }, {
313
- // Light theme
314
- name: 'theme-light',
315
- root: './src/theme-light',
316
- fallback: './src',
317
- custom: './src/custom/theme-light'
318
- }, {
319
- /* ... */
320
- }]
321
- };
322
- ```
323
-
324
- Use the component namespace:
325
-
326
- ```html
327
- <!-- src/index.html -->
328
- <html>
329
- <body>
330
- <x-theme-dark::button>Submit</theme-dark::button>
331
- <x-theme-light::button>Submit</theme-light::button>
332
- </body>
333
- </html>
334
- ```
335
-
336
- Of course, you can change this folder structure as you prefer according to your project requirements.
337
-
338
- ### Slots
339
-
340
- Your components can inject code in specific slots you define, and then you can fill this content when you use the component.
341
- Find below a simple example.
342
-
343
- Create the component:
344
-
345
- ```html
346
- <!-- src/modal.html -->
347
- <div class="modal">
348
- <div class="modal-header">
349
- <slot:header></slot:header>
350
- </div>
351
- <div class="modal-body">
352
- <slot:body></slot:body>
353
- </div>
354
- <div class="modal-footer">
355
- <slot:footer></slot:footer>
356
- </div>
357
- </div>
358
- ```
359
-
360
- Use the component:
361
-
362
- ```html
363
- <!-- src/index.html -->
364
- <x-modal>
365
- <fill:header>Header content</fill:header>
366
- <fill:body>Body content</fill:body>
367
- <fill:footer>Footer content</fill:footer>
368
- </x-modal>
369
- ```
370
-
371
- Result:
372
-
373
- ```html
374
- <!-- dist/index.html -->
375
- <div class="modal">
376
- <div class="modal-header">
377
- Header content
378
- </div>
379
- <div class="modal-body">
380
- Body content
381
- </div>
382
- <div class="modal-footer">
383
- Footer content
384
- </div>
385
- </div>
386
- ```
387
-
388
- By default, the content is replaced, but you can also prepend or append the content, or keep the default content by not filling the slot.
389
-
390
- Add some default content in the component:
391
-
392
- ```html
393
- <!-- src/modal.html -->
394
- <div class="modal">
395
- <div class="modal-header">
396
- <slot:header>Default header</slot:header>
397
- </div>
398
- <div class="modal-body">
399
- <slot:body>content</slot:body>
400
- </div>
401
- <div class="modal-footer">
402
- <slot:footer>Footer</slot:footer>
403
- </div>
404
- </div>
405
- ```
406
-
407
- ```html
408
- <!-- src/index.html -->
409
- <x-modal>
410
- <fill:body prepend>Prepend body</fill:body>
411
- <fill:footer append>content</fill:footer>
412
- </x-modal>
413
- ```
414
-
415
- Result:
416
-
417
- ```html
418
- <!-- dist/index.html -->
419
- <div class="modal">
420
- <div class="modal-header">
421
- Default header
422
- </div>
423
- <div class="modal-body">
424
- Prepend body content
425
- </div>
426
- <div class="modal-footer">
427
- Footer content
428
- </div>
429
- </div>
430
- ```
431
-
432
- ### Stacks
433
-
434
- You can push content to named stacks which can be rendered somewhere else in another place. This can be particularly useful for specifying any JavaScript or CSS required by your components.
435
-
436
- First of all define a `<stack>` anywhere in your code, for example:
437
-
438
- ```html
439
- <!-- src/index.html -->
440
- <html>
441
- <head>
442
- <stack name="styles"></stack>
443
- </head>
444
- <body>
445
-
446
- <x-modal>
447
- <fill:header>Header content</fill:header>
448
- <fill:body>Body content</fill:body>
449
- <fill:footer>Footer content</fill:footer>
450
- </x-modal>
451
-
452
- <stack name="scripts"></stack>
453
- </body>
454
- </html>
455
- ```
456
-
457
- Then in modal components, or any other child components, you can push content to this stack.
458
-
459
- ```html
460
- <!-- src/modal.html -->
461
- <div class="modal">
462
- <div class="modal-header">
463
- <slot:header></slot:header>
464
- </div>
465
- <div class="modal-body">
466
- <slot:body></slot:body>
467
- </div>
468
- <div class="modal-footer">
469
- <slot:footer></slot:footer>
470
- </div>
471
- </div>
472
-
473
- <push name="styles">
474
- <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.2/dist/css/bootstrap.min.css" rel="stylesheet">
475
- </push>
476
-
477
- <push name="scripts">
478
- <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.2/dist/js/bootstrap.bundle.min.js"></script>
479
- </push>
480
- ```
481
-
482
- The output will be:
483
-
484
- ```html
485
- <!-- dist/index.html -->
486
- <html>
487
- <head>
488
- <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.2/dist/css/bootstrap.min.css" rel="stylesheet">
489
- </head>
490
- <body>
491
- <div class="modal">
492
- <div class="modal-header">
493
- Header content
494
- </div>
495
- <div class="modal-body">
496
- Body content
497
- </div>
498
- <div class="modal-footer">
499
- Footer content
500
- </div>
501
- </div>
502
- <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.2/dist/js/bootstrap.bundle.min.js"></script>
503
- </body>
504
- </html>
505
- ```
506
-
507
- The `once` attribute allows you to push content only once per rendering cycle. For example, if you are rendering a given component within a loop, you may wish to only push the JavaScript and CSS the first time the component is rendered.
508
-
509
- Example.
510
-
511
- ```html
512
- <!-- src/modal.html -->
513
- <div class="modal">
514
- <!-- ... -->
515
- </div>
516
-
517
- <!-- The push content will be pushed only once in the stack -->
518
-
519
- <push name="styles" once>
520
- <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.2/dist/css/bootstrap.min.css" rel="stylesheet">
521
- </push>
522
-
523
- <push name="scripts" once>
524
- <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.2/dist/js/bootstrap.bundle.min.js"></script>
525
- </push>
526
- ```
527
-
528
- By default, the content is pushed in the stack in the given order.
529
- If you would like to prepend content onto the beginning of a stack, you should use the `prepend` attribute:
530
-
531
- ```html
532
- <push name="scripts">
533
- <!-- This will be second -->
534
- <script src="/example.js"></script>
535
- </push>
536
-
537
- <!-- Later... -->
538
-
539
- <push name="scripts" prepend>
540
- <!-- This will be first -->
541
- <script src="/example-2.js"></script>
542
- </push>
543
- ```
544
-
545
- ### Props
546
-
547
- Behind the `props` there is powerful [posthtml-expressions](https://github.com/posthtml/posthtml-expressions) plugin, with feature to pass `props` (locals) via attributes and manipulate them via `<script props>`.
548
-
549
- Let's see how it works with a few examples starting with a basic one.
550
-
551
- Create the component:
552
-
553
- ```html
554
- <!-- src/my-component.html -->
555
- <script props>
556
- module.exports = {
557
- prop: props.prop || 'Default prop value'
558
- }
559
- </script>
560
- <div>
561
- {{ prop }}
562
- </div>
563
- ```
564
-
565
- Use:
566
-
567
- ```html
568
- <x-my-component prop="Hello world!"></x-my-component>
569
- ```
570
-
571
- The output will be:
572
-
573
- ```html
574
- <div>
575
- Hello world!
576
- </div>
577
- ```
578
-
579
- Without passing `prop` via attribute then the output would be `Default prop value`, as shown below.
580
-
581
- Use component without passing prop:
582
-
583
- ```html
584
- <x-my-component></x-my-component>
585
- ```
586
-
587
- The output will be:
588
-
589
- ```html
590
- <div>
591
- Default prop value
592
- </div>
593
- ```
594
-
595
- In the `<script props>` you have access to passed props via object `props`, and you can add any logic you need inside it.
596
-
597
- Create the component:
598
-
599
- ```html
600
- <!-- src/modal.html -->
601
- <script props>
602
- module.exports = {
603
- title: props.title || 'Default title',
604
- size: props.size ? `modal-${props.size}` : '',
605
- items: Array.isArray(props.items) ? props.items.concat(['first', 'second']) : ['first', 'second']
606
- }
607
- </script>
608
- <div class="modal {{ size }}">
609
- <div class="modal-header">
610
- {{ title }}
611
- </div>
612
- <div class="modal-body">
613
- <each loop="item in items"><span>{{ item }}</span></each>
614
- </div>
615
- </div>
616
- ```
617
-
618
- Use:
619
-
620
- ```html
621
- <x-modal size="xl" title="My modal title" items='["third", "fourth"]' class="modal-custom"></x-modal>
622
- ```
623
-
624
- The output will be:
625
-
626
- ```html
627
- <div class="modal modal-custom modal-xl">
628
- <div class="modal-header">
629
- My modal title
630
- </div>
631
- <div class="modal-body">
632
- <span>first</span>
633
- <span>second</span>
634
- <span>third</span>
635
- <span>fourth</span>
636
- </div>
637
- </div>
638
- ```
639
-
640
- You can also notice how the `class` attribute is merged with `class` attribute of the first node. In the next section you will know more about this.
641
-
642
- You can change how attributes are merged with global props defined via options by passing a callback function used by lodash method [mergeWith](https://lodash.com/docs/4.17.15#mergeWith).
643
-
644
- By default, all props are scoped to the component, and are not available to nested components. You can however change this accordingly to your need.
645
- Let's see below example.
646
-
647
- Create a component:
648
-
649
- ```html
650
- <!-- src/child.html -->
651
- <script props>
652
- module.exports = {
653
- prop: props.prop || 'Default prop value'
654
- }
655
- </script>
656
- <div>
657
- Prop in child: {{ prop }}
658
- </div>
659
- ```
660
-
661
- Create another component that use the first one:
662
-
663
-
664
- ```html
665
- <!-- src/parent.html -->
666
- <script props>
667
- module.exports = {
668
- prop: props.prop || 'Default prop value'
669
- }
670
- </script>
671
- <div>
672
- Prop in parent: {{ prop }}
673
- <x-child></x-child>
674
- </div>
675
- ```
676
-
677
- Use:
678
-
679
- ```html
680
- <x-parent prop="My prop"></x-parent>
681
- ```
682
-
683
- The output will be:
684
-
685
- ```html
686
- <div>
687
- Prop in parent: My prop
688
- <div>
689
- Prop in child: Default prop value
690
- </div>
691
- </div>
692
- ```
693
-
694
- As you can see `prop` in `x-child` component are default value and not the one set via `x-parent`. Prepend `aware:` to the attribute name to pass the props to nested components.
695
-
696
-
697
- ```html
698
- <x-parent aware:prop="My prop"></x-parent>
699
- ```
700
-
701
- The output now will be:
702
-
703
- ```html
704
- <div>
705
- Prop in parent: My prop
706
- <div>
707
- Prop in child: My prop
708
- </div>
709
- </div>
710
- ```
711
-
712
- ### Attributes
713
-
714
- You can pass any attributes to your components and this will be added to the first node of your component,
715
- or to the node with an attribute named `attributes`. If you are familiar with VueJS this is the same as so called
716
- [fallthrough attribute](https://vuejs.org/guide/components/attrs.html), or with Laravel Blade is
717
- [component-attributes](https://laravel.com/docs/10.x/blade#component-attributes).
718
-
719
- By default `class` and `style` are merged with existing `class` and `style` attribute.
720
- All others attributes are override by default.
721
- Only attributes defined in [valid-attributes.js](https://github.com/thewebartisan7/posthtml-components/blob/main/src/valid-attributes.js)
722
- or not defined as `props` in the `<script props>`.
723
-
724
- As already seen in basic example:
725
-
726
- ```html
727
- <!-- src/button.html -->
728
- <script props>
729
- module.exports = {
730
- label: props.label || 'A button'
731
- }
732
- </script>
733
- <button type="button" class="btn">
734
- {{ label }}
735
- </button>
736
- ```
737
-
738
- Use the component:
739
-
740
- ```html
741
- <!-- src/index.html -->
742
- <x-button type="submit" class="btn-primary" label="My button"></x-button>
743
- ```
744
-
745
- Result:
746
-
747
- ```html
748
- <!-- dist/index.html -->
749
- <button type="submit" class="btn btn-primary">My button</button>
750
- ```
751
-
752
- As you may notice the `label` attribute is not added as attribute, since it's defined as a `props`.
753
-
754
- As said early, `class` and `style` are merged by default, if you want to override them, just prepend `override:` to the attribute name:
755
-
756
- ```html
757
- <!-- src/index.html -->
758
- <x-button type="submit" override:class="btn-custom" label="My button"></x-button>
759
- ```
760
-
761
- Result:
762
-
763
- ```html
764
- <!-- dist/index.html -->
765
- <button type="submit" class="btn-custom">My button</button>
766
- ```
767
-
768
- If you want to use another node and not the first one, then you can add the attribute `attributes` like shown below.
769
-
770
- ```html
771
- <!-- src/my-component.html -->
772
- <div class="first-node">
773
- <div class="second-node" attributes>
774
- Hello world!
775
- </div>
776
- </div>
777
- ```
778
-
779
- Use the component:
780
-
781
- ```html
782
- <!-- src/index.html -->
783
- <x-my-component class="my-class"></x-my-component>
784
- ```
785
-
786
- Result:
787
-
788
- ```html
789
- <!-- dist/index.html -->
790
- <div class="first-node">
791
- <div class="second-node my-class">
792
- Hello world!
793
- </div>
794
- </div>
795
- ```
796
-
797
- You can add custom rules how attributes are parsed, as behind the scene it's used [posthtml-attrs-parser](https://github.com/posthtml/posthtml-attrs-parser) plugin.
798
-
799
- ### Advanced attributes configurations
800
-
801
- If default configurations for valid attributes are not right for you, then you can configure them as explained below.
802
-
803
- ```js
804
- // index.js
805
- const { readFileSync, writeFileSync } = require('fs')
806
-
807
- const posthtml = require('posthtml')
808
- const components = require('posthtml-components')
809
-
810
- const options = {
811
- root: './src',
812
-
813
- // Add attributes to specific tag or override defaults
814
- elementAttributes: {
815
- DIV: (defaultAttributes) => {
816
- /* Add new one */
817
- defaultAttributes.push('custom-attribute-name');
818
-
819
- return defaultAttributes;
820
- },
821
- DIV: (defaultAttributes) => {
822
- /* Override all */
823
- defaultAttributes = ['custom-attribute-name', 'another-one'];
824
-
825
- return defaultAttributes;
826
- },
827
- },
828
-
829
- // Add attributes to all tags, use '*' as wildcard for attribute name that starts with
830
- safelistAttributes: [
831
- 'custom-attribute-name',
832
- 'attribute-name-start-with-*'
833
- ],
834
-
835
- // Remove attributes from all tags that support it
836
- blacklistAttributes: [
837
- 'role'
838
- ]
839
- }
840
-
841
- posthtml(components(options))
842
- .process(readFileSync('src/index.html', 'utf8'))
843
- .then((result) => writeFileSync('dist/index.html', result.html, 'utf8'))
844
- ```
845
-
846
- ## Examples
847
-
848
- You can work with `<slot>` and `<fill>` or you can create component for each block of your component, and you can also support both of them.
849
- You can find an example of this inside `docs-src/components/modal`. Below is a short explanation about the both approach.
850
-
851
- ### Using slots
852
-
853
- Let's suppose we want to create a component for [bootstrap modal](https://getbootstrap.com/docs/5.2/components/modal/). The code required is:
854
-
855
- ```html
856
- <!-- Modal HTML -->
857
- <div class="modal fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
858
- <div class="modal-dialog">
859
- <div class="modal-content">
860
- <div class="modal-header">
861
- <h1 class="modal-title fs-5" id="exampleModalLabel">Modal title</h1>
862
- <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
863
- </div>
864
- <div class="modal-body">
865
- ...
866
- </div>
867
- <div class="modal-footer">
868
- <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
869
- <button type="button" class="btn btn-primary">Save changes</button>
870
- </div>
871
- </div>
872
- </div>
873
- </div>
874
- ```
875
-
876
- There is almost three block of code: the header, the body and the footer.
877
- So we could create our component with three slot like below:
878
-
879
- ```html
880
- <!-- Modal component -->
881
- <div class="modal fade" tabindex="-1" aria-hidden="true">
882
- <div class="modal-dialog">
883
- <div class="modal-content">
884
- <div class="modal-header">
885
- <slot:header></slot:header>
886
- <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
887
- </div>
888
- <div class="modal-body">
889
- <slot:body></slot:body>
890
- </div>
891
- <div class="modal-footer">
892
- <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
893
- <slot:footer></slot:footer>
894
- </div>
895
- </div>
896
- </div>
897
- </div>
898
- ```
899
-
900
- In this case we can use it like:
901
-
902
- ```html
903
- <x-modal
904
- id="exampleModal"
905
- aria-labelledby="exampleModalLabel"
906
- >
907
- <slot:header>
908
- <h5 class="modal-title" id="exampleModalLabel">My modal</h5>
909
- </slot:header>
910
-
911
- <slot:body>
912
- Modal body content goes here...
913
- </slot:body>
914
-
915
- <slot:footer close="false">
916
- <button type="button" class="btn btn-primary">Confirm</button>
917
- </slot:footer>
918
- </x-modal>
919
- ```
920
-
921
- ### Splitting component in small component
922
-
923
- Another way is to split the component in small component, my preferred way, because you can pass attributes to each of them.
924
- So we create the component with a main component and then three different small component:
925
-
926
- ```html
927
- <!-- Main modal component -->
928
- <div class="modal fade" tabindex="-1" aria-hidden="true">
929
- <div class="modal-dialog">
930
- <div class="modal-content">
931
- <yield></yield>
932
- </div>
933
- </div>
934
- </div>
935
- ```
936
-
937
- ```html
938
- <!-- Header modal component -->
939
- <div class="modal-header">
940
- <yield></yield>
941
- </div>
942
- ```
943
-
944
- ```html
945
- <!-- Body modal component -->
946
- <div class="modal-body">
947
- <yield></yield>
948
- </div>
949
- ```
950
-
951
- ```html
952
- <!-- Footer modal component -->
953
- <div class="modal-footer">
954
- <yield></yield>
955
- </div>
956
- ```
957
-
958
- And then you can use it like below example:
959
-
960
- ```html
961
- <x-modal
962
- id="exampleModal"
963
- aria-labelledby="exampleModalLabel"
964
- >
965
- <x-modal.header>
966
- <h5 class="modal-title" id="exampleModalLabel">My modal</h5>
967
- </x-modal.header>
968
-
969
- <x-modal.body>
970
- Modal body content goes here...
971
- </x-modal.body>
972
-
973
- <x-modal.footer>
974
- <button type="button" class="btn btn-primary">Confirm</button>
975
- </x-modal.footer>
976
- </x-modal>
977
- ```
978
-
979
- As said in this way you can pass attributes to each of them, without defining props.
980
-
981
- ### Combine slots and small component
982
-
983
- You can also combine both way, and then use them with slots or with small component:
984
-
985
- ```html
986
-
987
- <!-- Modal -->
988
- <div
989
- class="modal fade"
990
- tabindex="-1"
991
- aria-hidden="true"
992
- aria-modal="true"
993
- role="dialog"
994
- >
995
- <div class="modal-dialog">
996
- <div class="modal-content">
997
- <if condition="$slots.header?.filled">
998
- <x-modal.header>
999
- <slot:header></slot:header>
1000
- </x-modal.header>
1001
- </if>
1002
- <if condition="$slots.body?.filled">
1003
- <x-modal.body>
1004
- <slot:body></slot:body>
1005
- </x-modal.body>
1006
- </if>
1007
- <if condition="$slots.footer?.filled">
1008
- <x-modal.footer close="{{ $slots.footer?.props.close }}">
1009
- <slot:footer></slot:footer>
1010
- </x-modal.footer>
1011
- </if>
1012
- <yield></yield>
1013
- </div>
1014
- </div><!-- /.modal-dialog -->
1015
- </div><!-- /.modal -->
1016
- ```
1017
-
1018
- Now you can use your component with slots or with small components.
1019
- As you may notice, by using slots, you already can use also your small components, and so you can also pass props
1020
- via `$slots` which has all the `props` passed via slot, and as well check if slot is filled.
1021
-
1022
- ## Migration
1023
-
1024
- If you are migrating from `posthtml-extend` and/or `posthtml-modules` please to follow updates here:
1025
- [posthtml-components/issues/16](https://github.com/thewebartisan7/posthtml-components/issues/16).
1026
-
1027
- ## Contributing
1028
-
1029
- See [PostHTML Guidelines](https://github.com/posthtml/posthtml/tree/master/docs) and [contribution guide](CONTRIBUTING.md).
1030
-
1031
- [npm]: https://img.shields.io/npm/v/posthtml-component.svg
1032
- [npm-url]: https://www.npmjs.com/package/posthtml-component
1033
-
1034
- [style]: https://img.shields.io/badge/code_style-XO-5ed9c7.svg
1035
- [style-url]: https://github.com/sindresorhus/xo
1036
-
1037
- [cover]: https://coveralls.io/repos/thewebartisan7/posthtml-components/badge.svg?branch=main
1038
- [cover-badge]: https://coveralls.io/r/thewebartisan7/posthtml-components?branch=main
1039
-
1040
- ## Credits
1041
-
1042
- Thanks to all PostHTML contributors and especially to `posthtml-extend` and `posthtml-modules` contributors, as part of code are ~~stolen~~ inspired from these plugins.
1043
- Huge thanks also to Laravel Blade template engine.
1
+ [![NPM][npm]][npm-url]
2
+ [![Coverage][cover]][cover-badge]
3
+ [![XO code style][style]][style-url]
4
+
5
+ <div align="center">
6
+ <img width="300" title="PostHTML" src="http://posthtml.github.io/posthtml/logo.svg">
7
+ <h1>PostHTML Components </h1>
8
+ <p>A PostHTML plugin for create components with HTML-friendly syntax inspired by Laravel Blade. Slots, stack/push, props, custom tag and much more.</p>
9
+ </div>
10
+
11
+ ## Installation
12
+
13
+ ```bash
14
+ npm i -D posthtml-component
15
+ ```
16
+
17
+ ## Introduction
18
+
19
+ This PostHTML plugin provides an HTML-friendly syntax for write components in your templates.
20
+ If you are familiar with Blade, React, Vue or similar, you will find familiar syntax as this plugin is inspired by them.
21
+ See below a basic example, as code is worth a thousand words.
22
+
23
+ **See also the first [PostHTML Bootstrap UI](https://github.com/thewebartisan7/posthtml-bootstrap-ui) using this plugin and check also the [starter template here](https://github.com/thewebartisan7/posthtml-bootstrap-ui-starter).**
24
+
25
+ ## Basic example
26
+
27
+ Create the component:
28
+
29
+ ``` html
30
+ <!-- src/button.html -->
31
+ <button type="button" class="btn">
32
+ <yield></yield>
33
+ </button>
34
+ ```
35
+
36
+ Use the component:
37
+
38
+ ``` html
39
+ <!-- src/index.html -->
40
+ <html>
41
+ <body>
42
+ <x-button type="submit" class="btn-primary">Submit</x-button>
43
+ </body>
44
+ </html>
45
+ ```
46
+
47
+ Init PostHTML:
48
+
49
+ ```js
50
+ // index.js
51
+ const { readFileSync, writeFileSync } = require('fs')
52
+
53
+ const posthtml = require('posthtml')
54
+ const components = require('posthtml-components')
55
+
56
+ posthtml(components({ root: './src' }))
57
+ .process(readFileSync('src/index.html', 'utf8'))
58
+ .then((result) => writeFileSync('dist/index.html', result.html, 'utf8'))
59
+ ```
60
+
61
+ Result:
62
+
63
+ ``` html
64
+ <!-- dist/index.html -->
65
+ <html>
66
+ <body>
67
+ <button type="submit" class="btn btn-primary">Submit</button>
68
+ </body>
69
+ </html>
70
+ ```
71
+
72
+ You may notice that the `src/button.html` component has a `type` and `class` attribute, and when we use the component in `src/index.html` we pass `type` and `class` attribute.
73
+ The result is that `type` is override, and `class` is merged.
74
+
75
+ By default `class` and `style` attributes are merged, while all others attribute are override.
76
+ You can also override `class` and `style` attributes by prepending `override:` to the class attribute. Example:
77
+
78
+ ```html
79
+ <x-button override:class="btn-custom">Submit</x-button>
80
+
81
+ <!-- Output -->
82
+ <button type="button" class="btn-custom">Submit</button>
83
+ ```
84
+
85
+ All attributes you pass to the component will be added to the first node of your component or to the node with an attribute names `attributes`,
86
+ and only if they are not defined as `props` via `<script props>` or if they are not in the following file
87
+ [valid-attributes.js](https://github.com/thewebartisan7/posthtml-components/blob/main/src/valid-attributes.js).
88
+ You can also manage valid attributes via options.
89
+ More details on this in [Attributes](#attributes) section.
90
+
91
+ The `<yield>` tag is where your content will be injected.
92
+ In next section you can find all available options and then examples for each feature.
93
+
94
+ See also the `docs-src` folder where you can find more examples.
95
+ You can run `npm run build` to compile them.
96
+
97
+ ## Options
98
+
99
+ | Option | Default | Description |
100
+ |:------------------------:|:---------------------------------------------:|:------------------------------------------------------------------------------------------------------------------------------|
101
+ | **root** | `'./'` | String value as root path for components lookup. |
102
+ | **folders** | `['']` | Array of additional multi folders path from `options.root` or any defined namespaces root, fallback or custom. |
103
+ | **tagPrefix** | `x-` | String for tag prefix. The plugin will use RegExp with this string. |
104
+ | **tag** | `false` | String or boolean value for component tag. Use this with `options.attribute`. Boolean only false. |
105
+ | **attribute** | `src` | String value for component attribute for set path. |
106
+ | **namespaces** | `[]` | Array of namespace's root path, fallback path and custom path for override. |
107
+ | **namespaceSeparator** | `::` | String value for namespace separator to be used with tag name. Example `<x-namespace::button>` |
108
+ | **fileExtension** | `html` | String value for file extension of the components used for retrieve x-tag file. |
109
+ | **yield** | `yield` | String value for `<yield>` tag name. Where main content of component is injected. |
110
+ | **slot** | `slot` | String value for `<slot>` tag name. Used with RegExp by appending `:` (example `<slot:slot-name>`). |
111
+ | **fill** | `fill` | String value for `<fill>` tag name. Used with RegExp by appending `:` (example `<fill:slot-name>`). |
112
+ | **slotSeparator** | `:` | String value used for separate `<slot>` and `<fill>` tag from their names. |
113
+ | **push** | `push` | String value for `<push>` tag name. |
114
+ | **stack** | `stack` | String value for `<stack>` tag name. |
115
+ | **propsScriptAttribute** | `props` | String value used as attribute in `<script props>` parsed by the plugin to retrieve props of the component. |
116
+ | **propsContext** | `props` | String value used as object name inside the script to process process before passed to the component. |
117
+ | **propsAttribute** | `props` | String value for props attribute to define props as JSON. |
118
+ | **propsSlot** | `props` | String value used to retrieve the props passed to slot via `$slots.slotName.props`. |
119
+ | **parserOptions** | `{recognizeSelfClosing: true}` | Object to configure `posthtml-parser`. By default, it enables support for self-closing component tags. |
120
+ | **expressions** | `{}` | Object to configure `posthtml-expressions`. You can pre-set locals or customize the delimiters for example. |
121
+ | **plugins** | `[]` | PostHTML plugins to apply for every parsed components. |
122
+ | **matcher** | `[{tag: options.tagPrefix}]` | Array of object used to match the tags. |
123
+ | **attrsParserRules** | `{}` | Additional rules for attributes parser plugin. |
124
+ | **strict** | `true` | Boolean value for enable or disable throw an exception. |
125
+ | **mergeCustomizer** | `function` | Function callback passed to lodash `mergeWith` for merge `options.expressions.locals` and props passed via attribute `props`. |
126
+ | **utilities** | `{merge: _.mergeWith, template: _.template}` | Object of utilities methods to be passed to `<script props>`. By default lodash `mergeWith` and `template`. |
127
+ | **elementAttributes** | `{}` | An object with tag name and a function modifier of valid-attributes.js. |
128
+ | **safelistAttributes** | `['data-*']` | An array of attributes name to be added to default valid attributes. |
129
+ | **blacklistAttributes** | `[]` | An array of attributes name to be removed from default valid attributes. |
130
+
131
+ ## Features
132
+
133
+ ### Tag names and x-tags
134
+
135
+ You can use the components in multiple ways, or also a combination of them.
136
+ Like with `posthtml-extend` and `posthtml-modules` you can define a tag name in combination with an attribute name for set the path of the components.
137
+
138
+ For example for the same button component `src/button.html` in the basic example we can define the tag name and attribute name and then use it in this way:
139
+
140
+ ``` html
141
+ <!-- src/index.html -->
142
+ <html>
143
+ <body>
144
+ <component src="button.html">Submit</component>
145
+ </body>
146
+ </html>
147
+ ```
148
+
149
+ Init PostHTML:
150
+
151
+ ```js
152
+ // index.js
153
+
154
+ require('posthtml')(require('posthtml-components')({ root: './src', tagName: 'component', attribute: 'src' }))
155
+ .process(/* ... */)
156
+ .then(/* ... */)
157
+ ```
158
+
159
+ If you need more control over how to match the tags, you can pass directly an array of matcher or single object via `options.matcher` like shown in below example:
160
+
161
+ ```js
162
+ // index.js
163
+
164
+ const options = {
165
+ root: './src',
166
+ matcher: [{tag: 'a-tag'}, {tag: 'another-one'}, {tag: new RegExp(`^app-`, 'i')}]
167
+ };
168
+
169
+ require('posthtml')(require('posthtml-components')(options))
170
+ .process(/* ... */)
171
+ .then(/* ... */)
172
+ ```
173
+
174
+ With `posthtml-components` you don't need to specify the path name when you are using `x-tag-name` syntax. See below example.
175
+
176
+ Setup PostHTML:
177
+
178
+ ```js
179
+ // index.js
180
+
181
+ const options = {
182
+ root: './src',
183
+ tagPrefix: 'x-'
184
+ };
185
+
186
+ require('posthtml')(require('posthtml-components')(options))
187
+ .process(/* ... */)
188
+ .then(/* ... */)
189
+ ```
190
+
191
+ Use:
192
+
193
+ ``` html
194
+ <!-- src/index.html -->
195
+ <html>
196
+ <body>
197
+ <x-button>Submit</x-button>
198
+ </body>
199
+ </html>
200
+ ```
201
+
202
+ If your components are in a subfolder then you can use `dot` to access it, example:
203
+
204
+ ``` html
205
+ <!-- Supposing your button component is located in ./src/components/forms/button.html -->
206
+ <x-forms.button>Submit</x-forms.button>
207
+ ```
208
+
209
+ If your components are in a sub-folder with multiple files, then for avoid typing the main file you can use `index.html` without specify it.
210
+ Please see below example to understand better.
211
+
212
+ ``` html
213
+ <!-- Supposing your modal component is located in ./src/components/modals/index.html -->
214
+ <x-modal.index>Submit</x-modal.index>
215
+
216
+ <!-- You can omit "index" part since the file is named "index.html" -->
217
+ <x-modal>Submit</x-modal>
218
+ ```
219
+
220
+ #### Parser options
221
+
222
+ You may pass options to `posthtml-parser` via `options.parserOptions`.
223
+
224
+ ```js
225
+ // index.js
226
+ const options = {
227
+ root: './src',
228
+ parserOptions: { decodeEntities: true }
229
+ };
230
+
231
+ require('posthtml')(require('posthtml-components')(options))
232
+ .process('some HTML', options.parserOptions)
233
+ .then(/* ... */)
234
+ ```
235
+
236
+ Important: as you can see, whatever `parserOptions` you pass to the plugin, must also be passed in the `process` method in your code, otherwise your PostHTML build will use `posthtml-parser` defaults and will override anything you've passed to `posthtml-component`.
237
+
238
+ #### Self-closing tags
239
+
240
+ The plugin supports self-closing tags by default, but you need to make sure to enable them in the `process` method in your code too, by passing `recognizeSelfClosing: true` in the options object:
241
+
242
+ ```js
243
+ // index.js
244
+ require('posthtml')(require('posthtml-components')({root: './src'}))
245
+ .process('your HTML...', {recognizeSelfClosing: true})
246
+ .then(/* ... */)
247
+ ```
248
+
249
+ If you don't add this to `process`, PostHTML will use `posthtml-parser` defaults and will not support self-closing component tags. This will result in everything after a self-closing tag not being output.
250
+
251
+ ### Multiple folders
252
+
253
+ You have full control where to place your components. Once you set the base root path of your components, you can then set multiple folders.
254
+ For example let's suppose your main root is `./src` and then you have several folders where you have your components, for example `./src/components` and `./src/layouts`.
255
+ You can set up the plugin like below:
256
+
257
+ ```js
258
+ // index.js
259
+ const options = {
260
+ root: './src',
261
+ folders: ['components', 'layouts']
262
+ };
263
+
264
+ require('posthtml')(require('posthtml-components')(options))
265
+ .process(/* ... */)
266
+ .then(/* ... */)
267
+ ```
268
+
269
+ ### Namespaces
270
+
271
+ With namespaces, you can define a top level root path to your components like shown in below example.
272
+ It can be useful for handle custom theme, where you define a specific top level root, with fallback root when component it's not found,
273
+ and a custom root for override, something like a child theme.
274
+
275
+ Thanks to namespace, you can create folders structure like below:
276
+
277
+ - `src` (base root folder)
278
+ - `components` (folder for components like modal, button, etc.)
279
+ - `layouts` (folder for layout components like base layout, header, footer, etc.)
280
+ - `theme-dark` (namespace folder for theme-dark)
281
+ - `components` (folder for components for theme dark)
282
+ - `layouts` (folder for layout components for dark theme)
283
+ - `theme-light` (namespace folder for theme-light)
284
+ - `components` (folder for components for light theme)
285
+ - `layouts` (folder for layout components for dark theme)
286
+ - `custom` (custom folder for override your namespace themes)
287
+ - `theme-dark` (custom folder for override dark theme)
288
+ - `components` (folder for override components of theme dark)
289
+ - `layouts` (folder for override layout components of dark theme)
290
+ - `theme-light` (custom folder for override light theme)
291
+ - `components` (folder for override components of theme dark)
292
+ - `layouts` (folder for override layout components of dark theme)
293
+
294
+ And the options would be like:
295
+
296
+ ```js
297
+ // index.js
298
+ const options = {
299
+ // Main root for component without namespace
300
+ root: './src',
301
+ // Folders is always appended in 'root' or any defined namespace's folders (base, fallback or custom)
302
+ folders: ['components', 'layouts'],
303
+ namespaces: [{
304
+ // Namespace name will be prepend to tag name (example <x-theme-dark::button>)
305
+ name: 'theme-dark',
306
+ // Base root of the namespace
307
+ root: './src/theme-dark',
308
+ // Fallback root when a component it's not found in namespace
309
+ fallback: './src',
310
+ // Custom root for override, the lookup happen first here
311
+ custom: './src/custom/theme-dark'
312
+ }, {
313
+ // Light theme
314
+ name: 'theme-light',
315
+ root: './src/theme-light',
316
+ fallback: './src',
317
+ custom: './src/custom/theme-light'
318
+ }, {
319
+ /* ... */
320
+ }]
321
+ };
322
+ ```
323
+
324
+ Use the component namespace:
325
+
326
+ ```html
327
+ <!-- src/index.html -->
328
+ <html>
329
+ <body>
330
+ <x-theme-dark::button>Submit</theme-dark::button>
331
+ <x-theme-light::button>Submit</theme-light::button>
332
+ </body>
333
+ </html>
334
+ ```
335
+
336
+ Of course, you can change this folder structure as you prefer according to your project requirements.
337
+
338
+ ### Slots
339
+
340
+ Your components can inject code in specific slots you define, and then you can fill this content when you use the component.
341
+ Find below a simple example.
342
+
343
+ Create the component:
344
+
345
+ ```html
346
+ <!-- src/modal.html -->
347
+ <div class="modal">
348
+ <div class="modal-header">
349
+ <slot:header></slot:header>
350
+ </div>
351
+ <div class="modal-body">
352
+ <slot:body></slot:body>
353
+ </div>
354
+ <div class="modal-footer">
355
+ <slot:footer></slot:footer>
356
+ </div>
357
+ </div>
358
+ ```
359
+
360
+ Use the component:
361
+
362
+ ```html
363
+ <!-- src/index.html -->
364
+ <x-modal>
365
+ <fill:header>Header content</fill:header>
366
+ <fill:body>Body content</fill:body>
367
+ <fill:footer>Footer content</fill:footer>
368
+ </x-modal>
369
+ ```
370
+
371
+ Result:
372
+
373
+ ```html
374
+ <!-- dist/index.html -->
375
+ <div class="modal">
376
+ <div class="modal-header">
377
+ Header content
378
+ </div>
379
+ <div class="modal-body">
380
+ Body content
381
+ </div>
382
+ <div class="modal-footer">
383
+ Footer content
384
+ </div>
385
+ </div>
386
+ ```
387
+
388
+ By default, the content is replaced, but you can also prepend or append the content, or keep the default content by not filling the slot.
389
+
390
+ Add some default content in the component:
391
+
392
+ ```html
393
+ <!-- src/modal.html -->
394
+ <div class="modal">
395
+ <div class="modal-header">
396
+ <slot:header>Default header</slot:header>
397
+ </div>
398
+ <div class="modal-body">
399
+ <slot:body>content</slot:body>
400
+ </div>
401
+ <div class="modal-footer">
402
+ <slot:footer>Footer</slot:footer>
403
+ </div>
404
+ </div>
405
+ ```
406
+
407
+ ```html
408
+ <!-- src/index.html -->
409
+ <x-modal>
410
+ <fill:body prepend>Prepend body</fill:body>
411
+ <fill:footer append>content</fill:footer>
412
+ </x-modal>
413
+ ```
414
+
415
+ Result:
416
+
417
+ ```html
418
+ <!-- dist/index.html -->
419
+ <div class="modal">
420
+ <div class="modal-header">
421
+ Default header
422
+ </div>
423
+ <div class="modal-body">
424
+ Prepend body content
425
+ </div>
426
+ <div class="modal-footer">
427
+ Footer content
428
+ </div>
429
+ </div>
430
+ ```
431
+
432
+ ### Stacks
433
+
434
+ You can push content to named stacks which can be rendered somewhere else in another place. This can be particularly useful for specifying any JavaScript or CSS required by your components.
435
+
436
+ First of all define a `<stack>` anywhere in your code, for example:
437
+
438
+ ```html
439
+ <!-- src/index.html -->
440
+ <html>
441
+ <head>
442
+ <stack name="styles"></stack>
443
+ </head>
444
+ <body>
445
+
446
+ <x-modal>
447
+ <fill:header>Header content</fill:header>
448
+ <fill:body>Body content</fill:body>
449
+ <fill:footer>Footer content</fill:footer>
450
+ </x-modal>
451
+
452
+ <stack name="scripts"></stack>
453
+ </body>
454
+ </html>
455
+ ```
456
+
457
+ Then in modal components, or any other child components, you can push content to this stack.
458
+
459
+ ```html
460
+ <!-- src/modal.html -->
461
+ <div class="modal">
462
+ <div class="modal-header">
463
+ <slot:header></slot:header>
464
+ </div>
465
+ <div class="modal-body">
466
+ <slot:body></slot:body>
467
+ </div>
468
+ <div class="modal-footer">
469
+ <slot:footer></slot:footer>
470
+ </div>
471
+ </div>
472
+
473
+ <push name="styles">
474
+ <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.2/dist/css/bootstrap.min.css" rel="stylesheet">
475
+ </push>
476
+
477
+ <push name="scripts">
478
+ <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.2/dist/js/bootstrap.bundle.min.js"></script>
479
+ </push>
480
+ ```
481
+
482
+ The output will be:
483
+
484
+ ```html
485
+ <!-- dist/index.html -->
486
+ <html>
487
+ <head>
488
+ <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.2/dist/css/bootstrap.min.css" rel="stylesheet">
489
+ </head>
490
+ <body>
491
+ <div class="modal">
492
+ <div class="modal-header">
493
+ Header content
494
+ </div>
495
+ <div class="modal-body">
496
+ Body content
497
+ </div>
498
+ <div class="modal-footer">
499
+ Footer content
500
+ </div>
501
+ </div>
502
+ <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.2/dist/js/bootstrap.bundle.min.js"></script>
503
+ </body>
504
+ </html>
505
+ ```
506
+
507
+ The `once` attribute allows you to push content only once per rendering cycle. For example, if you are rendering a given component within a loop, you may wish to only push the JavaScript and CSS the first time the component is rendered.
508
+
509
+ Example.
510
+
511
+ ```html
512
+ <!-- src/modal.html -->
513
+ <div class="modal">
514
+ <!-- ... -->
515
+ </div>
516
+
517
+ <!-- The push content will be pushed only once in the stack -->
518
+
519
+ <push name="styles" once>
520
+ <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.2/dist/css/bootstrap.min.css" rel="stylesheet">
521
+ </push>
522
+
523
+ <push name="scripts" once>
524
+ <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.2/dist/js/bootstrap.bundle.min.js"></script>
525
+ </push>
526
+ ```
527
+
528
+ By default, the content is pushed in the stack in the given order.
529
+ If you would like to prepend content onto the beginning of a stack, you should use the `prepend` attribute:
530
+
531
+ ```html
532
+ <push name="scripts">
533
+ <!-- This will be second -->
534
+ <script src="/example.js"></script>
535
+ </push>
536
+
537
+ <!-- Later... -->
538
+
539
+ <push name="scripts" prepend>
540
+ <!-- This will be first -->
541
+ <script src="/example-2.js"></script>
542
+ </push>
543
+ ```
544
+
545
+ ### Props
546
+
547
+ Behind the `props` there is powerful [posthtml-expressions](https://github.com/posthtml/posthtml-expressions) plugin, with feature to pass `props` (locals) via attributes and manipulate them via `<script props>`.
548
+
549
+ Let's see how it works with a few examples starting with a basic one.
550
+
551
+ Create the component:
552
+
553
+ ```html
554
+ <!-- src/my-component.html -->
555
+ <script props>
556
+ module.exports = {
557
+ prop: props.prop || 'Default prop value'
558
+ }
559
+ </script>
560
+ <div>
561
+ {{ prop }}
562
+ </div>
563
+ ```
564
+
565
+ Use:
566
+
567
+ ```html
568
+ <x-my-component prop="Hello world!"></x-my-component>
569
+ ```
570
+
571
+ The output will be:
572
+
573
+ ```html
574
+ <div>
575
+ Hello world!
576
+ </div>
577
+ ```
578
+
579
+ Without passing `prop` via attribute then the output would be `Default prop value`, as shown below.
580
+
581
+ Use component without passing prop:
582
+
583
+ ```html
584
+ <x-my-component></x-my-component>
585
+ ```
586
+
587
+ The output will be:
588
+
589
+ ```html
590
+ <div>
591
+ Default prop value
592
+ </div>
593
+ ```
594
+
595
+ In the `<script props>` you have access to passed props via object `props`, and you can add any logic you need inside it.
596
+
597
+ Create the component:
598
+
599
+ ```html
600
+ <!-- src/modal.html -->
601
+ <script props>
602
+ module.exports = {
603
+ title: props.title || 'Default title',
604
+ size: props.size ? `modal-${props.size}` : '',
605
+ items: Array.isArray(props.items) ? props.items.concat(['first', 'second']) : ['first', 'second']
606
+ }
607
+ </script>
608
+ <div class="modal {{ size }}">
609
+ <div class="modal-header">
610
+ {{ title }}
611
+ </div>
612
+ <div class="modal-body">
613
+ <each loop="item in items"><span>{{ item }}</span></each>
614
+ </div>
615
+ </div>
616
+ ```
617
+
618
+ Use:
619
+
620
+ ```html
621
+ <x-modal size="xl" title="My modal title" items='["third", "fourth"]' class="modal-custom"></x-modal>
622
+ ```
623
+
624
+ The output will be:
625
+
626
+ ```html
627
+ <div class="modal modal-custom modal-xl">
628
+ <div class="modal-header">
629
+ My modal title
630
+ </div>
631
+ <div class="modal-body">
632
+ <span>first</span>
633
+ <span>second</span>
634
+ <span>third</span>
635
+ <span>fourth</span>
636
+ </div>
637
+ </div>
638
+ ```
639
+
640
+ You can also notice how the `class` attribute is merged with `class` attribute of the first node. In the next section you will know more about this.
641
+
642
+ You can change how attributes are merged with global props defined via options by passing a callback function used by lodash method [mergeWith](https://lodash.com/docs/4.17.15#mergeWith).
643
+
644
+ By default, all props are scoped to the component, and are not available to nested components. You can however change this accordingly to your need.
645
+ Let's see below example.
646
+
647
+ Create a component:
648
+
649
+ ```html
650
+ <!-- src/child.html -->
651
+ <script props>
652
+ module.exports = {
653
+ prop: props.prop || 'Default prop value'
654
+ }
655
+ </script>
656
+ <div>
657
+ Prop in child: {{ prop }}
658
+ </div>
659
+ ```
660
+
661
+ Create another component that use the first one:
662
+
663
+
664
+ ```html
665
+ <!-- src/parent.html -->
666
+ <script props>
667
+ module.exports = {
668
+ prop: props.prop || 'Default prop value'
669
+ }
670
+ </script>
671
+ <div>
672
+ Prop in parent: {{ prop }}
673
+ <x-child></x-child>
674
+ </div>
675
+ ```
676
+
677
+ Use:
678
+
679
+ ```html
680
+ <x-parent prop="My prop"></x-parent>
681
+ ```
682
+
683
+ The output will be:
684
+
685
+ ```html
686
+ <div>
687
+ Prop in parent: My prop
688
+ <div>
689
+ Prop in child: Default prop value
690
+ </div>
691
+ </div>
692
+ ```
693
+
694
+ As you can see `prop` in `x-child` component are default value and not the one set via `x-parent`. Prepend `aware:` to the attribute name to pass the props to nested components.
695
+
696
+
697
+ ```html
698
+ <x-parent aware:prop="My prop"></x-parent>
699
+ ```
700
+
701
+ The output now will be:
702
+
703
+ ```html
704
+ <div>
705
+ Prop in parent: My prop
706
+ <div>
707
+ Prop in child: My prop
708
+ </div>
709
+ </div>
710
+ ```
711
+
712
+ ### Attributes
713
+
714
+ You can pass any attributes to your components and this will be added to the first node of your component,
715
+ or to the node with an attribute named `attributes`. If you are familiar with VueJS this is the same as so called
716
+ [fallthrough attribute](https://vuejs.org/guide/components/attrs.html), or with Laravel Blade is
717
+ [component-attributes](https://laravel.com/docs/10.x/blade#component-attributes).
718
+
719
+ By default `class` and `style` are merged with existing `class` and `style` attribute.
720
+ All others attributes are override by default.
721
+ Only attributes defined in [valid-attributes.js](https://github.com/thewebartisan7/posthtml-components/blob/main/src/valid-attributes.js)
722
+ or not defined as `props` in the `<script props>`.
723
+
724
+ As already seen in basic example:
725
+
726
+ ```html
727
+ <!-- src/button.html -->
728
+ <script props>
729
+ module.exports = {
730
+ label: props.label || 'A button'
731
+ }
732
+ </script>
733
+ <button type="button" class="btn">
734
+ {{ label }}
735
+ </button>
736
+ ```
737
+
738
+ Use the component:
739
+
740
+ ```html
741
+ <!-- src/index.html -->
742
+ <x-button type="submit" class="btn-primary" label="My button"></x-button>
743
+ ```
744
+
745
+ Result:
746
+
747
+ ```html
748
+ <!-- dist/index.html -->
749
+ <button type="submit" class="btn btn-primary">My button</button>
750
+ ```
751
+
752
+ As you may notice the `label` attribute is not added as attribute, since it's defined as a `props`.
753
+
754
+ As said early, `class` and `style` are merged by default, if you want to override them, just prepend `override:` to the attribute name:
755
+
756
+ ```html
757
+ <!-- src/index.html -->
758
+ <x-button type="submit" override:class="btn-custom" label="My button"></x-button>
759
+ ```
760
+
761
+ Result:
762
+
763
+ ```html
764
+ <!-- dist/index.html -->
765
+ <button type="submit" class="btn-custom">My button</button>
766
+ ```
767
+
768
+ If you want to use another node and not the first one, then you can add the attribute `attributes` like shown below.
769
+
770
+ ```html
771
+ <!-- src/my-component.html -->
772
+ <div class="first-node">
773
+ <div class="second-node" attributes>
774
+ Hello world!
775
+ </div>
776
+ </div>
777
+ ```
778
+
779
+ Use the component:
780
+
781
+ ```html
782
+ <!-- src/index.html -->
783
+ <x-my-component class="my-class"></x-my-component>
784
+ ```
785
+
786
+ Result:
787
+
788
+ ```html
789
+ <!-- dist/index.html -->
790
+ <div class="first-node">
791
+ <div class="second-node my-class">
792
+ Hello world!
793
+ </div>
794
+ </div>
795
+ ```
796
+
797
+ You can add custom rules how attributes are parsed, as behind the scene it's used [posthtml-attrs-parser](https://github.com/posthtml/posthtml-attrs-parser) plugin.
798
+
799
+ ### Advanced attributes configurations
800
+
801
+ If default configurations for valid attributes are not right for you, then you can configure them as explained below.
802
+
803
+ ```js
804
+ // index.js
805
+ const { readFileSync, writeFileSync } = require('fs')
806
+
807
+ const posthtml = require('posthtml')
808
+ const components = require('posthtml-components')
809
+
810
+ const options = {
811
+ root: './src',
812
+
813
+ // Add attributes to specific tag or override defaults
814
+ elementAttributes: {
815
+ DIV: (defaultAttributes) => {
816
+ /* Add new one */
817
+ defaultAttributes.push('custom-attribute-name');
818
+
819
+ return defaultAttributes;
820
+ },
821
+ DIV: (defaultAttributes) => {
822
+ /* Override all */
823
+ defaultAttributes = ['custom-attribute-name', 'another-one'];
824
+
825
+ return defaultAttributes;
826
+ },
827
+ },
828
+
829
+ // Add attributes to all tags, use '*' as wildcard for attribute name that starts with
830
+ safelistAttributes: [
831
+ 'custom-attribute-name',
832
+ 'attribute-name-start-with-*'
833
+ ],
834
+
835
+ // Remove attributes from all tags that support it
836
+ blacklistAttributes: [
837
+ 'role'
838
+ ]
839
+ }
840
+
841
+ posthtml(components(options))
842
+ .process(readFileSync('src/index.html', 'utf8'))
843
+ .then((result) => writeFileSync('dist/index.html', result.html, 'utf8'))
844
+ ```
845
+
846
+ ## Examples
847
+
848
+ You can work with `<slot>` and `<fill>` or you can create component for each block of your component, and you can also support both of them.
849
+ You can find an example of this inside `docs-src/components/modal`. Below is a short explanation about the both approach.
850
+
851
+ ### Using slots
852
+
853
+ Let's suppose we want to create a component for [bootstrap modal](https://getbootstrap.com/docs/5.2/components/modal/). The code required is:
854
+
855
+ ```html
856
+ <!-- Modal HTML -->
857
+ <div class="modal fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
858
+ <div class="modal-dialog">
859
+ <div class="modal-content">
860
+ <div class="modal-header">
861
+ <h1 class="modal-title fs-5" id="exampleModalLabel">Modal title</h1>
862
+ <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
863
+ </div>
864
+ <div class="modal-body">
865
+ ...
866
+ </div>
867
+ <div class="modal-footer">
868
+ <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
869
+ <button type="button" class="btn btn-primary">Save changes</button>
870
+ </div>
871
+ </div>
872
+ </div>
873
+ </div>
874
+ ```
875
+
876
+ There is almost three block of code: the header, the body and the footer.
877
+ So we could create our component with three slot like below:
878
+
879
+ ```html
880
+ <!-- Modal component -->
881
+ <div class="modal fade" tabindex="-1" aria-hidden="true">
882
+ <div class="modal-dialog">
883
+ <div class="modal-content">
884
+ <div class="modal-header">
885
+ <slot:header></slot:header>
886
+ <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
887
+ </div>
888
+ <div class="modal-body">
889
+ <slot:body></slot:body>
890
+ </div>
891
+ <div class="modal-footer">
892
+ <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
893
+ <slot:footer></slot:footer>
894
+ </div>
895
+ </div>
896
+ </div>
897
+ </div>
898
+ ```
899
+
900
+ In this case we can use it like:
901
+
902
+ ```html
903
+ <x-modal
904
+ id="exampleModal"
905
+ aria-labelledby="exampleModalLabel"
906
+ >
907
+ <slot:header>
908
+ <h5 class="modal-title" id="exampleModalLabel">My modal</h5>
909
+ </slot:header>
910
+
911
+ <slot:body>
912
+ Modal body content goes here...
913
+ </slot:body>
914
+
915
+ <slot:footer close="false">
916
+ <button type="button" class="btn btn-primary">Confirm</button>
917
+ </slot:footer>
918
+ </x-modal>
919
+ ```
920
+
921
+ ### Splitting component in small component
922
+
923
+ Another way is to split the component in small component, my preferred way, because you can pass attributes to each of them.
924
+ So we create the component with a main component and then three different small component:
925
+
926
+ ```html
927
+ <!-- Main modal component -->
928
+ <div class="modal fade" tabindex="-1" aria-hidden="true">
929
+ <div class="modal-dialog">
930
+ <div class="modal-content">
931
+ <yield></yield>
932
+ </div>
933
+ </div>
934
+ </div>
935
+ ```
936
+
937
+ ```html
938
+ <!-- Header modal component -->
939
+ <div class="modal-header">
940
+ <yield></yield>
941
+ </div>
942
+ ```
943
+
944
+ ```html
945
+ <!-- Body modal component -->
946
+ <div class="modal-body">
947
+ <yield></yield>
948
+ </div>
949
+ ```
950
+
951
+ ```html
952
+ <!-- Footer modal component -->
953
+ <div class="modal-footer">
954
+ <yield></yield>
955
+ </div>
956
+ ```
957
+
958
+ And then you can use it like below example:
959
+
960
+ ```html
961
+ <x-modal
962
+ id="exampleModal"
963
+ aria-labelledby="exampleModalLabel"
964
+ >
965
+ <x-modal.header>
966
+ <h5 class="modal-title" id="exampleModalLabel">My modal</h5>
967
+ </x-modal.header>
968
+
969
+ <x-modal.body>
970
+ Modal body content goes here...
971
+ </x-modal.body>
972
+
973
+ <x-modal.footer>
974
+ <button type="button" class="btn btn-primary">Confirm</button>
975
+ </x-modal.footer>
976
+ </x-modal>
977
+ ```
978
+
979
+ As said in this way you can pass attributes to each of them, without defining props.
980
+
981
+ ### Combine slots and small component
982
+
983
+ You can also combine both way, and then use them with slots or with small component:
984
+
985
+ ```html
986
+
987
+ <!-- Modal -->
988
+ <div
989
+ class="modal fade"
990
+ tabindex="-1"
991
+ aria-hidden="true"
992
+ aria-modal="true"
993
+ role="dialog"
994
+ >
995
+ <div class="modal-dialog">
996
+ <div class="modal-content">
997
+ <if condition="$slots.header?.filled">
998
+ <x-modal.header>
999
+ <slot:header></slot:header>
1000
+ </x-modal.header>
1001
+ </if>
1002
+ <if condition="$slots.body?.filled">
1003
+ <x-modal.body>
1004
+ <slot:body></slot:body>
1005
+ </x-modal.body>
1006
+ </if>
1007
+ <if condition="$slots.footer?.filled">
1008
+ <x-modal.footer close="{{ $slots.footer?.props.close }}">
1009
+ <slot:footer></slot:footer>
1010
+ </x-modal.footer>
1011
+ </if>
1012
+ <yield></yield>
1013
+ </div>
1014
+ </div><!-- /.modal-dialog -->
1015
+ </div><!-- /.modal -->
1016
+ ```
1017
+
1018
+ Now you can use your component with slots or with small components.
1019
+ As you may notice, by using slots, you already can use also your small components, and so you can also pass props
1020
+ via `$slots` which has all the `props` passed via slot, and as well check if slot is filled.
1021
+
1022
+ ## Migration
1023
+
1024
+ If you are migrating from `posthtml-extend` and/or `posthtml-modules` please to follow updates here:
1025
+ [posthtml-components/issues/16](https://github.com/thewebartisan7/posthtml-components/issues/16).
1026
+
1027
+ ## Contributing
1028
+
1029
+ See [PostHTML Guidelines](https://github.com/posthtml/posthtml/tree/master/docs) and [contribution guide](CONTRIBUTING.md).
1030
+
1031
+ [npm]: https://img.shields.io/npm/v/posthtml-component.svg
1032
+ [npm-url]: https://www.npmjs.com/package/posthtml-component
1033
+
1034
+ [style]: https://img.shields.io/badge/code_style-XO-5ed9c7.svg
1035
+ [style-url]: https://github.com/sindresorhus/xo
1036
+
1037
+ [cover]: https://coveralls.io/repos/thewebartisan7/posthtml-components/badge.svg?branch=main
1038
+ [cover-badge]: https://coveralls.io/r/thewebartisan7/posthtml-components?branch=main
1039
+
1040
+ ## Credits
1041
+
1042
+ Thanks to all PostHTML contributors and especially to `posthtml-extend` and `posthtml-modules` contributors, as part of code are ~~stolen~~ inspired from these plugins.
1043
+ Huge thanks also to Laravel Blade template engine.