Skip to content

Commit 7b16829

Browse files
Remove computed: and merge: and using always script props for locals
1 parent 76c35fa commit 7b16829

24 files changed

+215
-669
lines changed

docs/demo.html

+3-3
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,9 @@ <h1 class="display-1 fw-bold mb-4">Demo</h1>
6868
</div>
6969
</div>
7070
</footer>
71-
<div class="modal fade" id="modalWithComponents" data-bs-backdrop="true" data-bs-keyboard="true" aria-labelledby="modalWithComponents" tabindex="-1" aria-hidden="true" aria-modal="true" role="dialog">
72-
<div class="modal-dialog modal-lg modal-fullscreen-lg-down modal-dialog-centered modal-dialog-custom">
73-
<div class="modal-content modal-content-custom">
71+
<div class="modal fade" id="modalWithComponents" data-bs-backdrop="true" data-bs-keyboard="true" aria-labelledby="modalWithComponents" tabindex="-1" aria-hidden="true" aria-modal="true" role="dialog" dialogclass="modal-dialog-custom" contentclass="modal-content-custom">
72+
<div class="modal-dialog modal-lg modal-fullscreen-lg-down modal-dialog-centered">
73+
<div class="modal-content">
7474
<div class="modal-header">
7575
<h5 class="modal-title" id="modalWithComponentsLabel">Changelog</h5>
7676
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>

docs/docs.html

+24-146
Original file line numberDiff line numberDiff line change
@@ -181,23 +181,6 @@
181181
</li>
182182
<li class="nav-item">
183183
<a class="nav-link" href="#migration">Migration</a>
184-
<ul class="nav flex-column">
185-
<li class="nav-item">
186-
<a class="nav-link" href="#posthtml-include">PostHTML Include</a>
187-
</li>
188-
<li class="nav-item">
189-
<a class="nav-link" href="#posthtml-modules">PostHTML Modules</a>
190-
</li>
191-
<li class="nav-item">
192-
<a class="nav-link" href="#posthtml-extends">PostHTML Extends</a>
193-
</li>
194-
</ul>
195-
</li>
196-
<li class="nav-item">
197-
<a class="nav-link" href="#contributing">Contributing</a>
198-
</li>
199-
<li class="nav-item">
200-
<a class="nav-link" href="#credits">Credits</a>
201184
</li>
202185
</ul>
203186
</nav>
@@ -562,11 +545,11 @@ <h2 id="options" tabindex="-1">
562545
<td style="text-align:left">
563546
Function callback passed to lodash
564547
<code>mergeWith</code>
565-
for attribute
548+
for merge
549+
<code>options.expressions.locals</code>
550+
and locals passed via attribute
566551
<code>locals</code>
567-
and
568-
<code>merge:attribute</code>
569-
. By default it's used to concat array.
552+
.
570553
</td>
571554
</tr>
572555
</tbody>
@@ -1006,18 +989,16 @@ <h3 id="props" tabindex="-1">
1006989
<a href="https://github.com/posthtml/posthtml-expressions">posthtml-expressions</a>
1007990
plugin, with feature to pass
1008991
<code>props</code>
1009-
(locals) via attributes, define default via
1010-
<code>&lt;script props&gt;</code>
1011-
, merge with default and use
992+
(locals) via attributes and manipulate them via
1012993
<code>&lt;script props&gt;</code>
1013-
as computed.
994+
.
1014995
</p>
1015996
<p>Let's see how it works with a few examples starting with a basic one.</p>
1016997
<p>Create the component:</p>
1017998
<pre><code class="language-html">&lt;!-- src/my-component.html --&gt;
1018999
&lt;script props&gt;
10191000
module.exports = {
1020-
prop: 'Default prop value'
1001+
prop: locals.prop || 'Default prop value'
10211002
}
10221003
&lt;/script&gt;
10231004
&lt;div&gt;
@@ -1051,24 +1032,19 @@ <h3 id="props" tabindex="-1">
10511032
More details on this in the next section.
10521033
</p>
10531034
<p>
1054-
So by default
1035+
So in the
10551036
<code>&lt;script props&gt;</code>
1056-
act as default value, like the
1057-
<code>@props</code>
1058-
you define with Laravel Blade.
1059-
You can change this behaviour by prepending
1060-
<code>computed</code>
1061-
or
1062-
<code>merge</code>
1063-
to the attribute name like shown below.
1037+
you have access to passed props via object
1038+
<code>locals</code>
1039+
, and you can add any logic you need inside of it.
10641040
</p>
10651041
<p>Create the component:</p>
10661042
<pre><code class="language-html">&lt;!-- src/modal.html --&gt;
10671043
&lt;script props&gt;
10681044
module.exports = {
1069-
title: 'Default title', // This will be the default value
1070-
size: locals.size ? `modal-${locals.size}` : '', // This will be a computed value, so it's always used
1071-
items: ['first', 'second'] // This will be merged
1045+
title: locals.title || 'Default title',
1046+
size: locals.size ? `modal-${locals.size}` : '',
1047+
items: Array.isArray(locals.items) ? locals.items.concat(['first', 'second']) : ['first', 'second']
10721048
}
10731049
&lt;/script&gt;
10741050
&lt;div class=&quot;modal {{ size }}&quot;&gt;
@@ -1080,7 +1056,7 @@ <h3 id="props" tabindex="-1">
10801056
&lt;/div&gt;
10811057
&lt;/div&gt;</code></pre>
10821058
<p>Use:</p>
1083-
<pre><code class="language-html">&lt;x-modal computed:size=&quot;xl&quot; title=&quot;My modal title&quot; merge:items='[&quot;third&quot;, &quot;fourth&quot;]' class=&quot;modal-custom&quot;&gt;&lt;/x-modal&gt;</code></pre>
1059+
<pre><code class="language-html">&lt;x-modal size=&quot;xl&quot; title=&quot;My modal title&quot; items='[&quot;third&quot;, &quot;fourth&quot;]' class=&quot;modal-custom&quot;&gt;&lt;/x-modal&gt;</code></pre>
10841060
<p>The output will be:</p>
10851061
<pre><code class="language-html">&lt;div class=&quot;modal modal-custom modal-xl&quot;&gt;
10861062
&lt;div class=&quot;modal-header&quot;&gt;
@@ -1094,27 +1070,16 @@ <h3 id="props" tabindex="-1">
10941070
&lt;/div&gt;
10951071
&lt;/div&gt;</code></pre>
10961072
<p>
1097-
So the prop
1098-
<code>size</code>
1099-
is not override since we prepend
1100-
<code>computed:</code>
1101-
to the attribute, while the prop
1102-
<code>title</code>
1103-
is override.
1104-
And the prop
1105-
<code>items</code>
1106-
is merged and not override.
1107-
You can also notice how the
1073+
You can also notice how the
11081074
<code>class</code>
11091075
attribute is merged with
11101076
<code>class</code>
11111077
attribute of the first node. Let's see in next section more about this.
11121078
</p>
11131079
<p>
1114-
You can change how attributes are merged by passing via options a callback function used by lodash method
1080+
You can change how attributes are merged with global locals defined via options by passing a callback function used by lodash method
11151081
<a href="https://lodash.com/docs/4.17.15#mergeWith">mergeWith</a>
11161082
.
1117-
By default, it's used to concat array.
11181083
</p>
11191084
<p>
11201085
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.
@@ -1124,7 +1089,7 @@ <h3 id="props" tabindex="-1">
11241089
<pre><code class="language-html">&lt;!-- src/child.html --&gt;
11251090
&lt;script props&gt;
11261091
module.exports = {
1127-
prop: 'Default prop value'
1092+
prop: locals.prop || 'Default prop value'
11281093
}
11291094
&lt;/script&gt;
11301095
&lt;div&gt;
@@ -1134,7 +1099,7 @@ <h3 id="props" tabindex="-1">
11341099
<pre><code class="language-html">&lt;!-- src/parent.html --&gt;
11351100
&lt;script props&gt;
11361101
module.exports = {
1137-
prop: 'Default prop value'
1102+
prop: locals.prop || 'Default prop value'
11381103
}
11391104
&lt;/script&gt;
11401105
&lt;div&gt;
@@ -1195,7 +1160,7 @@ <h3 id="attributes" tabindex="-1">
11951160
<pre><code class="language-html">&lt;!-- src/button.html --&gt;
11961161
&lt;script props&gt;
11971162
module.exports = {
1198-
label: 'A button'
1163+
label: locals.label || 'A button'
11991164
}
12001165
&lt;/script&gt;
12011166
&lt;button type=&quot;button&quot; class=&quot;btn&quot;&gt;
@@ -1437,97 +1402,10 @@ <h2 id="migration" tabindex="-1">
14371402
<code>posthtml-extend</code>
14381403
and/or
14391404
<code>posthtml-modules</code>
1440-
then you can continue to keep them until you migrate all of your components.
1441-
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:
1405+
please to follow updates here:
14421406
<a href="https://github.com/thewebartisan7/posthtml-components/issues/16">posthtml-components/issues/16</a>
14431407
.
14441408
</p>
1445-
<h3 id="posthtml-include" tabindex="-1">
1446-
<a class="header-anchor" href="#posthtml-include">#</a>
1447-
PostHTML Include
1448-
</h3>
1449-
<p>
1450-
PostHTML Include plugin can work when passed via
1451-
<code>options.plugins</code>
1452-
like below example:
1453-
</p>
1454-
<pre><code class="language-js">require(&quot;posthtml-component&quot;)({
1455-
root: &quot;./src&quot;,
1456-
folders: [&quot;components&quot;, &quot;layouts&quot;],
1457-
plugins: [
1458-
require(&quot;posthtml-include&quot;)({
1459-
encoding: &quot;utf8&quot;,
1460-
root: &quot;./src&quot;
1461-
}),
1462-
]
1463-
})</code></pre>
1464-
<h3 id="posthtml-modules" tabindex="-1">
1465-
<a class="header-anchor" href="#posthtml-modules">#</a>
1466-
PostHTML Modules
1467-
</h3>
1468-
<p>
1469-
At the moment doesn't work when nested inside PostHTML Components plugin since it use
1470-
<code>tree.match</code>
1471-
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:
1472-
</p>
1473-
<pre><code class="language-js">require(&quot;posthtml-component&quot;)({
1474-
root: &quot;./src&quot;,
1475-
folders: [&quot;components&quot;, &quot;layouts&quot;],
1476-
tag: 'module',
1477-
attribute: 'href',
1478-
yield: 'content',
1479-
plugins: [
1480-
require(&quot;posthtml-include&quot;)({
1481-
encoding: &quot;utf8&quot;,
1482-
root: &quot;./src/www/posthtml-templates/&quot;
1483-
}),
1484-
]
1485-
})</code></pre>
1486-
<p>
1487-
NOTE: If you change
1488-
<code>&lt;yield&gt;</code>
1489-
tag to
1490-
<code>&lt;content&gt;</code>
1491-
to support your existing code, then you need to use it always. Maybe you can just replace all
1492-
<code>&lt;content&gt;</code>
1493-
with
1494-
<code>&lt;yield&gt;</code>
1495-
and it should works fine.
1496-
</p>
1497-
<h3 id="posthtml-extends" tabindex="-1">
1498-
<a class="header-anchor" href="#posthtml-extends">#</a>
1499-
PostHTML Extends
1500-
</h3>
1501-
<p>
1502-
So far it works fine together with
1503-
<code>posthtml-components</code>
1504-
plugin.
1505-
</p>
1506-
<h2 id="contributing" tabindex="-1">
1507-
<a class="header-anchor" href="#contributing">#</a>
1508-
Contributing
1509-
</h2>
1510-
<p>
1511-
See
1512-
<a href="https://github.com/posthtml/posthtml/tree/master/docs">PostHTML Guidelines</a>
1513-
and
1514-
<a href="CONTRIBUTING.md">contribution guide</a>
1515-
.
1516-
</p>
1517-
<h2 id="credits" tabindex="-1">
1518-
<a class="header-anchor" href="#credits">#</a>
1519-
Credits
1520-
</h2>
1521-
<p>
1522-
Thanks to all PostHTML contributors and especially to
1523-
<code>posthtml-extend</code>
1524-
and
1525-
<code>posthtml-modules</code>
1526-
contributors, as part of code are
1527-
<s>stolen</s>
1528-
inspired from these plugins.
1529-
Huge thanks also to Laravel Blade template engine.
1530-
</p>
15311409
</div>
15321410
</div>
15331411
</div>
@@ -1549,9 +1427,9 @@ <h2 id="credits" tabindex="-1">
15491427
</div>
15501428
</div>
15511429
</footer>
1552-
<div class="modal fade" id="modalWithComponents" data-bs-backdrop="true" data-bs-keyboard="true" aria-labelledby="modalWithComponents" tabindex="-1" aria-hidden="true" aria-modal="true" role="dialog">
1553-
<div class="modal-dialog modal-lg modal-fullscreen-lg-down modal-dialog-centered modal-dialog-custom">
1554-
<div class="modal-content modal-content-custom">
1430+
<div class="modal fade" id="modalWithComponents" data-bs-backdrop="true" data-bs-keyboard="true" aria-labelledby="modalWithComponents" tabindex="-1" aria-hidden="true" aria-modal="true" role="dialog" dialogclass="modal-dialog-custom" contentclass="modal-content-custom">
1431+
<div class="modal-dialog modal-lg modal-fullscreen-lg-down modal-dialog-centered">
1432+
<div class="modal-content">
15551433
<div class="modal-header">
15561434
<h5 class="modal-title" id="modalWithComponentsLabel">Changelog</h5>
15571435
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>

docs/index.html

+3-3
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,9 @@ <h1 class="display-1 fw-bold mb-4">Build the web with PostHTML</h1>
8484
</div>
8585
</div>
8686
</footer>
87-
<div class="modal fade" id="modalWithComponents" data-bs-backdrop="true" data-bs-keyboard="true" aria-labelledby="modalWithComponents" tabindex="-1" aria-hidden="true" aria-modal="true" role="dialog">
88-
<div class="modal-dialog modal-lg modal-fullscreen-lg-down modal-dialog-centered modal-dialog-custom">
89-
<div class="modal-content modal-content-custom">
87+
<div class="modal fade" id="modalWithComponents" data-bs-backdrop="true" data-bs-keyboard="true" aria-labelledby="modalWithComponents" tabindex="-1" aria-hidden="true" aria-modal="true" role="dialog" dialogclass="modal-dialog-custom" contentclass="modal-content-custom">
88+
<div class="modal-dialog modal-lg modal-fullscreen-lg-down modal-dialog-centered">
89+
<div class="modal-content">
9090
<div class="modal-header">
9191
<h5 class="modal-title" id="modalWithComponentsLabel">Changelog</h5>
9292
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>

0 commit comments

Comments
 (0)