158
158
</ ul >
159
159
</ li >
160
160
< li class ="nav-item ">
161
- < a class ="nav-link " href ="#tips-and-tricks " > Tips and tricks </ a >
161
+ < a class ="nav-link " href ="#examples " > Examples </ a >
162
162
< ul class ="nav flex-column ">
163
163
< li class ="nav-item ">
164
164
< a class ="nav-link " href ="#using-slots "> Using slots</ a >
173
173
</ li >
174
174
< li class ="nav-item ">
175
175
< a class ="nav-link " href ="#migration "> Migration</ a >
176
+ < ul class ="nav flex-column ">
177
+ < li class ="nav-item ">
178
+ < a class ="nav-link " href ="#posthtml-include "> PostHTML Include</ a >
179
+ </ li >
180
+ < li class ="nav-item ">
181
+ < a class ="nav-link " href ="#posthtml-modules "> PostHTML Modules</ a >
182
+ </ li >
183
+ < li class ="nav-item ">
184
+ < a class ="nav-link " href ="#posthtml-extends "> PostHTML Extends</ a >
185
+ </ li >
186
+ </ ul >
176
187
</ li >
177
188
</ ul >
178
189
</ nav >
@@ -424,6 +435,21 @@ <h2 id="options" tabindex="-1">
424
435
).
425
436
</ td >
426
437
</ tr >
438
+ < tr >
439
+ < td style ="text-align:center ">
440
+ < strong > slotSeparator</ strong >
441
+ </ td >
442
+ < td style ="text-align:center ">
443
+ < code > :</ code >
444
+ </ td >
445
+ < td style ="text-align:left ">
446
+ String value used for separate
447
+ < code > <slot></ code >
448
+ and
449
+ < code > <fill></ code >
450
+ tag from their names.
451
+ </ td >
452
+ </ tr >
427
453
< tr >
428
454
< td style ="text-align:center ">
429
455
< strong > push</ strong >
@@ -949,20 +975,69 @@ <h3 id="props" tabindex="-1">
949
975
< a href ="https://github.com/posthtml/posthtml-expressions "> posthtml-expressions</ a >
950
976
plugin, with feature to pass
951
977
< code > props</ code >
952
- via attributes, define default via
978
+ (locals) via attributes, define default via
953
979
< code > <script props></ code >
954
980
, merge with default and use
955
981
< code > <script props></ code >
956
982
as computed.
957
983
</ p >
958
- < p > Let's see how it works with an example.</ p >
984
+ < p > Let's see how it works with a few examples starting with a basic one.</ p >
985
+ < p > Create the component:</ p >
986
+ < pre > < code class ="language-html "> <!-- src/my-component.html -->
987
+ <script props>
988
+ module.exports = {
989
+ prop: 'Default prop value'
990
+ }
991
+ </script>
992
+ <div>
993
+ {{ prop }}
994
+ </div></ code > </ pre >
995
+ < p > Use:</ p >
996
+ < pre > < code class ="language-html "> <x-my-component prop="Hello world!"></x-my-component></ code > </ pre >
997
+ < p > The output will be:</ p >
998
+ < pre > < code class ="language-html "> <div>
999
+ Hello world!
1000
+ </div></ code > </ pre >
1001
+ < p >
1002
+ Without passing
1003
+ < code > prop</ code >
1004
+ via attribute then the output would be
1005
+ < code > Default prop value</ code >
1006
+ , as shown below.
1007
+ </ p >
1008
+ < p > Use component without passing prop:</ p >
1009
+ < pre > < code class ="language-html "> <x-my-component></x-my-component></ code > </ pre >
1010
+ < p > The output will be:</ p >
1011
+ < pre > < code class ="language-html "> <div>
1012
+ Default prop value
1013
+ </div></ code > </ pre >
1014
+ < p >
1015
+ If you don't add the props in
1016
+ < code > <script props></ code >
1017
+ inside your component, all props will be added as attributes to the first node of your component or to the node with attribute
1018
+ < code > attributes</ code >
1019
+ .
1020
+ More details on this in the next section.
1021
+ </ p >
1022
+ < p >
1023
+ So by default
1024
+ < code > <script props></ code >
1025
+ act as default value, like the
1026
+ < code > @props</ code >
1027
+ you define with Laravel Blade.
1028
+ You can change this behaviour by prepending
1029
+ < code > computed</ code >
1030
+ or
1031
+ < code > merge</ code >
1032
+ to the attribute name like shown below.
1033
+ </ p >
959
1034
< p > Create the component:</ p >
960
1035
< pre > < code class ="language-html "> <!-- src/modal.html -->
961
1036
<script props>
962
1037
module.exports = {
963
- title: 'Default title',
964
- size: locals.size ? `modal-${locals.size}` : '',
965
- items: ['first', 'second']
1038
+ title: 'Default title', // This will be the default value
1039
+ size: locals.size ? `modal-${locals.size}` : '', // This will be a computed value, so it's always used
1040
+ items: ['first', 'second'] // This will be merged
966
1041
}
967
1042
</script>
968
1043
<div class="modal {{ size }}">
@@ -998,13 +1073,20 @@ <h3 id="props" tabindex="-1">
998
1073
And the prop
999
1074
< code > items</ code >
1000
1075
is merged and not override.
1076
+ You can also notice how the
1077
+ < code > class</ code >
1078
+ attribute is merged with
1079
+ < code > class</ code >
1080
+ attribute of the first node. Let's see in next section more about this.
1001
1081
</ p >
1002
1082
< h3 id ="attributes " tabindex ="-1 ">
1003
1083
< a class ="header-anchor " href ="#attributes "> #</ a >
1004
1084
Attributes
1005
1085
</ h3 >
1006
1086
< p >
1007
- Your can pass any attributes to your components and this will be added to the first element of your component.
1087
+ 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
1088
+ < code > attributes</ code >
1089
+ .
1008
1090
By default
1009
1091
< code > class</ code >
1010
1092
and
@@ -1013,11 +1095,11 @@ <h3 id="attributes" tabindex="-1">
1013
1095
< code > class</ code >
1014
1096
and
1015
1097
< code > style</ code >
1016
- attribute of the first element of your component .
1098
+ attribute.
1017
1099
All others attributes are override by default.
1018
1100
Only attribute not defined as
1019
1101
< code > props</ code >
1020
- will be processed .
1102
+ will be used .
1021
1103
</ p >
1022
1104
< p > As already seen in basic example:</ p >
1023
1105
< pre > < code class ="language-html "> <!-- src/button.html -->
@@ -1044,23 +1126,48 @@ <h3 id="attributes" tabindex="-1">
1044
1126
</ p >
1045
1127
< p > If you are familiar with Laravel Blade, this is also how Blade handle this.</ p >
1046
1128
< p >
1047
- As said early, class and style are merged by default, if you want to override them, just prepend
1129
+ As said early,
1130
+ < code > class</ code >
1131
+ and
1132
+ < code > style</ code >
1133
+ are merged by default, if you want to override them, just prepend
1048
1134
< code > override:</ code >
1049
- to the attributes :
1135
+ to the attribute name :
1050
1136
</ p >
1051
1137
< pre > < code class ="language-html "> <!-- src/index.html -->
1052
1138
<x-button type="submit" override:class="btn-custom" label="My button"></x-button></ code > </ pre >
1053
1139
< p > Result:</ p >
1054
1140
< pre > < code class ="language-html "> <!-- dist/index.html -->
1055
1141
<button type="submit" class="btn-custom">My button</button></ code > </ pre >
1142
+ < p >
1143
+ If you want to use another node for add such attributes, then you can add the attribute
1144
+ < code > attributes</ code >
1145
+ like shown below.
1146
+ </ p >
1147
+ < pre > < code class ="language-html "> <!-- src/my-component.html -->
1148
+ <div class="first-node">
1149
+ <div class="second-node" attributes>
1150
+ Hello world!
1151
+ </div>
1152
+ </div></ code > </ pre >
1153
+ < p > Use the component:</ p >
1154
+ < pre > < code class ="language-html "> <!-- src/index.html -->
1155
+ <x-my-component class="my-class"></x-my-component></ code > </ pre >
1156
+ < p > Result:</ p >
1157
+ < pre > < code class ="language-html "> <!-- dist/index.html -->
1158
+ <div class="first-node">
1159
+ <div class="second-node my-class">
1160
+ Hello world!
1161
+ </div>
1162
+ </div></ code > </ pre >
1056
1163
< p >
1057
1164
You can add custom rules how attributes are parsed, as behind the scene it's used
1058
1165
< a href ="https://github.com/posthtml/posthtml-attrs-parser "> posthtml-attrs-parser</ a >
1059
1166
plugin.
1060
1167
</ p >
1061
- < h2 id ="tips-and-tricks " tabindex ="-1 ">
1062
- < a class ="header-anchor " href ="#tips-and-tricks "> #</ a >
1063
- Tips and tricks
1168
+ < h2 id ="examples " tabindex ="-1 ">
1169
+ < a class ="header-anchor " href ="#examples "> #</ a >
1170
+ Examples
1064
1171
</ h2 >
1065
1172
< p >
1066
1173
You can work with
@@ -1241,9 +1348,67 @@ <h2 id="migration" tabindex="-1">
1241
1348
and/or
1242
1349
< code > posthtml-modules</ code >
1243
1350
then you can continue to keep them until you migrate all of your components.
1244
- For continue to use current code with this plugin without changing it, at the moment it's not yet fully supported, but it maybe comes in near future.
1245
- This because the new version has different logic to handle slots and locals, as well the yield content.
1351
+ For continue to use current code with this plugin without changing it, see below examples. Any more updates on this will be added in this issue:
1352
+ < a href ="https://github.com/thewebartisan7/posthtml-components/issues/16 "> posthtml-components/issues/16</ a >
1353
+ .
1354
+ </ p >
1355
+ < h3 id ="posthtml-include " tabindex ="-1 ">
1356
+ < a class ="header-anchor " href ="#posthtml-include "> #</ a >
1357
+ PostHTML Include
1358
+ </ h3 >
1359
+ < p >
1360
+ PostHTML Include plugin can work when passed via
1361
+ < code > options.plugins</ code >
1362
+ like below example:
1246
1363
</ p >
1364
+ < pre > < code class ="language-js "> require("posthtml-component")({
1365
+ root: "./src",
1366
+ folders: ["components", "layouts"],
1367
+ plugins: [
1368
+ require("posthtml-include")({
1369
+ encoding: "utf8",
1370
+ root: "./src"
1371
+ }),
1372
+ ]
1373
+ })</ code > </ pre >
1374
+ < h3 id ="posthtml-modules " tabindex ="-1 ">
1375
+ < a class ="header-anchor " href ="#posthtml-modules "> #</ a >
1376
+ PostHTML Modules
1377
+ </ h3 >
1378
+ < p >
1379
+ At the moment doesn't work when nested inside PostHTML Components plugin since it use
1380
+ < code > tree.match</ code >
1381
+ and even trying with something like PostHTML Include is doing here https://github.com/posthtml/posthtml-include/blob/master/lib/index.js#L16 doesn't work. But a workaround is to use PostHTML Components custom tag and attributes like below:
1382
+ </ p >
1383
+ < pre > < code class ="language-js "> require("posthtml-component")({
1384
+ root: "./src",
1385
+ folders: ["components", "layouts"],
1386
+ tag: 'module',
1387
+ attribute: 'href',
1388
+ yield: 'content'
1389
+ plugins: [
1390
+ require("posthtml-include")({
1391
+ encoding: "utf8",
1392
+ root: "./src/www/posthtml-templates/"
1393
+ }),
1394
+ ]
1395
+ })</ code > </ pre >
1396
+ < p >
1397
+ NOTE: If you change
1398
+ < code > <yield></ code >
1399
+ tag to
1400
+ < code > <content></ code >
1401
+ to support your existing code, then you need to use it always. Maybe you can just replace all
1402
+ < code > <content></ code >
1403
+ with
1404
+ < code > <yield></ code >
1405
+ and it should works fine.
1406
+ </ p >
1407
+ < h3 id ="posthtml-extends " tabindex ="-1 ">
1408
+ < a class ="header-anchor " href ="#posthtml-extends "> #</ a >
1409
+ PostHTML Extends
1410
+ </ h3 >
1411
+ < p > Not yet tested.</ p >
1247
1412
</ div >
1248
1413
</ div >
1249
1414
</ div >
0 commit comments