162
162
< li class ="nav-item ">
163
163
< a class ="nav-link " href ="#attributes "> Attributes</ a >
164
164
</ li >
165
+ < li class ="nav-item ">
166
+ < a class ="nav-link " href ="#advanced-attributes-configurations "> Advanced attributes configurations</ a >
167
+ </ li >
165
168
</ ul >
166
169
</ li >
167
170
< li class ="nav-item ">
183
186
</ li >
184
187
</ ul >
185
188
</ nav >
186
- < blockquote >
187
- < p >
188
- This plugin is still in early stage of development and the current API may change.
189
- </ p >
190
- </ blockquote >
191
189
< h2 id ="installation " tabindex ="-1 ">
192
190
< a class ="header-anchor " href ="#installation "> #</ a >
193
191
Installation
@@ -199,7 +197,7 @@ <h2 id="introduction" tabindex="-1">
199
197
</ h2 >
200
198
< p >
201
199
This PostHTML plugin provides an HTML-friendly syntax for write components in your templates.
202
- If you are familiar with Blade, you will find similar syntax as this plugin is inspired by it .
200
+ If you are familiar with Blade, React, Vue or similar, you will find familiar syntax as this plugin is inspired by them .
203
201
See below a basic example, as code is worth a thousand words.
204
202
</ p >
205
203
< p >
@@ -245,15 +243,19 @@ <h2 id="basic-example" tabindex="-1">
245
243
</body>
246
244
</html></ code > </ pre >
247
245
< p >
248
- You may notice that our
246
+ You may notice that the
249
247
< code > src/button.html</ code >
250
248
component has a
251
249
< code > type</ code >
252
250
and
253
251
< code > class</ code >
254
252
attribute, and when we use the component in
255
253
< code > src/index.html</ code >
256
- we add type and class attribute.
254
+ we pass
255
+ < code > type</ code >
256
+ and
257
+ < code > class</ code >
258
+ attribute.
257
259
The result is that
258
260
< code > type</ code >
259
261
is override, and
@@ -266,7 +268,11 @@ <h2 id="basic-example" tabindex="-1">
266
268
and
267
269
< code > style</ code >
268
270
attributes are merged, while all others attribute are override.
269
- You can also override class and style attribute by prepending
271
+ You can also override
272
+ < code > class</ code >
273
+ and
274
+ < code > style</ code >
275
+ attributes by prepending
270
276
< code > override:</ code >
271
277
to the class attribute. Example:
272
278
</ p >
@@ -282,20 +288,25 @@ <h2 id="basic-example" tabindex="-1">
282
288
< code > props</ code >
283
289
via
284
290
< code > <script props></ code >
285
- . More details on this in
291
+ or if they are not in the following file
292
+ < a href ="https://github.com/thewebartisan7/posthtml-components/blob/main/src/valid-attributes.js "> valid-attributes.js</ a >
293
+ .
294
+ You can also manage valid attributes via options.
295
+ More details on this in
286
296
< a href ="#attributes "> Attributes</ a >
287
297
section.
288
298
</ p >
289
299
< p >
290
300
The
291
301
< code > <yield></ code >
292
302
tag is where your content will be injected.
293
- In next section you can find all available options and then examples for each features .
303
+ In next section you can find all available options and then examples for each feature .
294
304
</ p >
295
305
< p >
296
306
See also the
297
307
< code > docs-src</ code >
298
- folder where you can find more examples. You can run
308
+ folder where you can find more examples.
309
+ You can run
299
310
< code > npm run build</ code >
300
311
to compile them.
301
312
</ p >
@@ -612,6 +623,33 @@ <h2 id="options" tabindex="-1">
612
623
.
613
624
</ td >
614
625
</ tr >
626
+ < tr >
627
+ < td style ="text-align:center ">
628
+ < strong > elementAttributes</ strong >
629
+ </ td >
630
+ < td style ="text-align:center ">
631
+ < code > {}</ code >
632
+ </ td >
633
+ < td style ="text-align:left "> An object with tag name and a function modifier of valid-attributes.js.</ td >
634
+ </ tr >
635
+ < tr >
636
+ < td style ="text-align:center ">
637
+ < strong > safelistAttributes</ strong >
638
+ </ td >
639
+ < td style ="text-align:center ">
640
+ < code > ['data-*']</ code >
641
+ </ td >
642
+ < td style ="text-align:left "> An array of attributes name to be added to default valid attributes.</ td >
643
+ </ tr >
644
+ < tr >
645
+ < td style ="text-align:center ">
646
+ < strong > blacklistAttributes</ strong >
647
+ </ td >
648
+ < td style ="text-align:center ">
649
+ < code > []</ code >
650
+ </ td >
651
+ < td style ="text-align:left "> An array of attributes name to be removed from default valid attributes.</ td >
652
+ </ tr >
615
653
</ tbody >
616
654
</ table >
617
655
< h2 id ="features " tabindex ="-1 ">
@@ -718,7 +756,7 @@ <h3 id="multiple-folders" tabindex="-1">
718
756
and
719
757
< code > ./src/layouts</ code >
720
758
.
721
- You can setup the plugin like below:
759
+ You can set up the plugin like below:
722
760
</ p >
723
761
< pre > < code class ="language-js "> // index.js
724
762
const options = {
@@ -734,7 +772,7 @@ <h3 id="namespaces" tabindex="-1">
734
772
Namespaces
735
773
</ h3 >
736
774
< p >
737
- With namespaces you can define a top level root path to your components like shown in below example.
775
+ With namespaces, you can define a top level root path to your components like shown in below example.
738
776
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,
739
777
and a custom root for override, something like a child theme.
740
778
</ p >
@@ -896,7 +934,7 @@ <h3 id="slots" tabindex="-1">
896
934
</div>
897
935
</div></ code > </ pre >
898
936
< p >
899
- 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.
937
+ 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.
900
938
</ p >
901
939
< p > Add some default content in the component:</ p >
902
940
< pre > < code class ="language-html "> <!-- src/modal.html -->
@@ -1022,7 +1060,7 @@ <h3 id="stacks" tabindex="-1">
1022
1060
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.2/dist/js/bootstrap.bundle.min.js"></script>
1023
1061
</push></ code > </ pre >
1024
1062
< p >
1025
- By default the content is pushed in the stack in the given order.
1063
+ By default, the content is pushed in the stack in the given order.
1026
1064
If you would like to prepend content onto the beginning of a stack, you should use the
1027
1065
< code > prepend</ code >
1028
1066
attribute:
@@ -1084,15 +1122,7 @@ <h3 id="props" tabindex="-1">
1084
1122
Default prop value
1085
1123
</div></ code > </ pre >
1086
1124
< p >
1087
- If you don't add the props in
1088
- < code > <script props></ code >
1089
- inside your component, all props will be added as attributes to the first node of your component or to the node with attribute
1090
- < code > attributes</ code >
1091
- .
1092
- More details on this in the next section.
1093
- </ p >
1094
- < p >
1095
- So in the
1125
+ In the
1096
1126
< code > <script props></ code >
1097
1127
you have access to passed props via object
1098
1128
< code > props</ code >
@@ -1134,7 +1164,7 @@ <h3 id="props" tabindex="-1">
1134
1164
< code > class</ code >
1135
1165
attribute is merged with
1136
1166
< code > class</ code >
1137
- attribute of the first node. Let's see in next section more about this.
1167
+ attribute of the first node. In the next section you will know more about this.
1138
1168
</ p >
1139
1169
< p >
1140
1170
You can change how attributes are merged with global props defined via options by passing a callback function used by lodash method
@@ -1199,10 +1229,17 @@ <h3 id="attributes" tabindex="-1">
1199
1229
Attributes
1200
1230
</ h3 >
1201
1231
< p >
1202
- Your can pass any attributes to your components and this will be added to the first node of your component, or to the node with an attribute named
1232
+ You can pass any attributes to your components and this will be added to the first node of your component,
1233
+ or to the node with an attribute named
1203
1234
< code > attributes</ code >
1235
+ . If you are familiar with VueJS this is the same as so called
1236
+ < a href ="https://vuejs.org/guide/components/attrs.html "> fallthrough attribute</ a >
1237
+ , or with Laravel Blade is
1238
+ < a href ="https://laravel.com/docs/10.x/blade#component-attributes "> component-attributes</ a >
1204
1239
.
1205
- By default
1240
+ </ p >
1241
+ < p >
1242
+ By default
1206
1243
< code > class</ code >
1207
1244
and
1208
1245
< code > style</ code >
@@ -1212,9 +1249,13 @@ <h3 id="attributes" tabindex="-1">
1212
1249
< code > style</ code >
1213
1250
attribute.
1214
1251
All others attributes are override by default.
1215
- Only attribute not defined as
1252
+ Only attributes defined in
1253
+ < a href ="https://github.com/thewebartisan7/posthtml-components/blob/main/src/valid-attributes.js "> valid-attributes.js</ a >
1254
+ or not defined as
1216
1255
< code > props</ code >
1217
- will be used.
1256
+ in the
1257
+ < code > <script props></ code >
1258
+ .
1218
1259
</ p >
1219
1260
< p > As already seen in basic example:</ p >
1220
1261
< pre > < code class ="language-html "> <!-- src/button.html -->
@@ -1239,7 +1280,6 @@ <h3 id="attributes" tabindex="-1">
1239
1280
< code > props</ code >
1240
1281
.
1241
1282
</ p >
1242
- < p > If you are familiar with Laravel Blade, this is also how Blade handle this.</ p >
1243
1283
< p >
1244
1284
As said early,
1245
1285
< code > class</ code >
@@ -1255,7 +1295,7 @@ <h3 id="attributes" tabindex="-1">
1255
1295
< pre > < code class ="language-html "> <!-- dist/index.html -->
1256
1296
<button type="submit" class="btn-custom">My button</button></ code > </ pre >
1257
1297
< p >
1258
- If you want to use another node for add such attributes , then you can add the attribute
1298
+ If you want to use another node and not the first one , then you can add the attribute
1259
1299
< code > attributes</ code >
1260
1300
like shown below.
1261
1301
</ p >
@@ -1280,6 +1320,53 @@ <h3 id="attributes" tabindex="-1">
1280
1320
< a href ="https://github.com/posthtml/posthtml-attrs-parser "> posthtml-attrs-parser</ a >
1281
1321
plugin.
1282
1322
</ p >
1323
+ < h3 id ="advanced-attributes-configurations " tabindex ="-1 ">
1324
+ < a class ="header-anchor " href ="#advanced-attributes-configurations "> #</ a >
1325
+ Advanced attributes configurations
1326
+ </ h3 >
1327
+ < p >
1328
+ If default configurations for valid attributes are not right for you, then you can configure them as explained below.
1329
+ </ p >
1330
+ < pre > < code class ="language-js "> // index.js
1331
+ const { readFileSync, writeFileSync } = require('fs')
1332
+
1333
+ const posthtml = require('posthtml')
1334
+ const components = require('posthtml-components')
1335
+
1336
+ const options = {
1337
+ root: './src',
1338
+
1339
+ // Add attributes to specific tag or override defaults
1340
+ elementAttributes: {
1341
+ DIV: (defaultAttributes) => {
1342
+ /* Add new one */
1343
+ defaultAttributes.push('custom-attribute-name');
1344
+
1345
+ return defaultAttributes;
1346
+ },
1347
+ DIV: (defaultAttributes) => {
1348
+ /* Override all */
1349
+ defaultAttributes = ['custom-attribute-name', 'another-one'];
1350
+
1351
+ return defaultAttributes;
1352
+ },
1353
+ },
1354
+
1355
+ // Add attributes to all tags, use '*' as wildcard for attribute name that starts with
1356
+ safelistAttributes: [
1357
+ 'custom-attribute-name',
1358
+ 'attribute-name-start-with-*'
1359
+ ],
1360
+
1361
+ // Remove attributes from all tags that support it
1362
+ blacklistAttributes: [
1363
+ 'role'
1364
+ ]
1365
+ }
1366
+
1367
+ posthtml(components(options))
1368
+ .process(readFileSync('src/index.html', 'utf8'))
1369
+ .then((result) => writeFileSync('dist/index.html', result.html, 'utf8'))</ code > </ pre >
1283
1370
< h2 id ="examples " tabindex ="-1 ">
1284
1371
< a class ="header-anchor " href ="#examples "> #</ a >
1285
1372
Examples
@@ -1289,7 +1376,7 @@ <h2 id="examples" tabindex="-1">
1289
1376
< code > <slot></ code >
1290
1377
and
1291
1378
< code > <fill></ code >
1292
- or you can create component for each " block" of your component, and you can also support both of them.
1379
+ or you can create component for each block of your component, and you can also support both of them.
1293
1380
You can find an example of this inside
1294
1381
< code > docs-src/components/modal</ code >
1295
1382
. Below is a short explanation about the both approach.
@@ -1470,7 +1557,7 @@ <h2 id="migration" tabindex="-1">
1470
1557
</ div >
1471
1558
</ div >
1472
1559
</ main >
1473
- < footer >
1560
+ < footer class =" footer mt-auto py-3 " >
1474
1561
< div class ="container-lg ">
1475
1562
< div class ="text-muted text-center ">
1476
1563
Handcrafted with
@@ -1496,6 +1583,15 @@ <h5 class="modal-title" id="modalWithComponentsLabel">Changelog</h5>
1496
1583
</ div >
1497
1584
<!-- /.modal-header -->
1498
1585
< div class ="modal-body ">
1586
+ < h2 id ="1.0.0-rc.1-(2023-03-08) " tabindex ="-1 ">
1587
+ < a class ="header-anchor " href ="#1.0.0-rc.1-(2023-03-08) "> #</ a >
1588
+ 1.0.0-RC.1 (2023-03-08)
1589
+ </ h2 >
1590
+ < p > First Release Candidate</ p >
1591
+ < ul >
1592
+ < li > Prevent attributes used for passing props from being added to first node</ li >
1593
+ < li > Updated docs</ li >
1594
+ </ ul >
1499
1595
< h2 id ="1.0.0-beta.16-(2022-11-06) " tabindex ="-1 ">
1500
1596
< a class ="header-anchor " href ="#1.0.0-beta.16-(2022-11-06) "> #</ a >
1501
1597
1.0.0-beta.16 (2022-11-06)
0 commit comments