Skip to content

Commit e35f0cf

Browse files
committed
feat: add :parent directive
1 parent fd84d4e commit e35f0cf

File tree

4 files changed

+108
-12
lines changed

4 files changed

+108
-12
lines changed

index.html

+10-7
Original file line numberDiff line numberDiff line change
@@ -1505,13 +1505,13 @@ <h3 class="font-bold font-mono">Multi Select:</h3>
15051505
grid-template-rows: 0fr 1fr;
15061506
}
15071507
</style>
1508-
<div class="accordion">
1508+
<div class="accordion" :parent>
15091509
<section
15101510
class="grid transition-all border-gray-300 border border-b-0 rounded hover:bg-gray-100"
1511-
:class="activeSection =='about' ? 'active' : ''"
1511+
:class="parent.activeSection =='about' ? 'active' : ''"
15121512
>
15131513
<a
1514-
:click="activeSection='about'"
1514+
:click="parent.activeSection='about'"
15151515
class="cursor-pointer font-bold p-4"
15161516
>
15171517
About Us
@@ -1526,10 +1526,10 @@ <h3 class="font-bold font-mono">Multi Select:</h3>
15261526

15271527
<section
15281528
class="grid transition-all border-gray-300 border border-b-0 rounded hover:bg-gray-100"
1529-
:class="activeSection =='contact' ? 'active' : ''"
1529+
:class="parent.activeSection =='contact' ? 'active' : ''"
15301530
>
15311531
<a
1532-
:click="activeSection='contact'"
1532+
:click="parent.activeSection='contact'"
15331533
class="cursor-pointer font-bold p-4"
15341534
>
15351535
Contact Us
@@ -1544,9 +1544,12 @@ <h3 class="font-bold font-mono">Multi Select:</h3>
15441544

15451545
<section
15461546
class="grid transition-all border-gray-300 border rounded hover:bg-gray-100"
1547-
:class="activeSection =='team' ? 'active' : ''"
1547+
:class="parent.activeSection =='team' ? 'active' : ''"
15481548
>
1549-
<a :click="activeSection='team'" class="cursor-pointer font-bold p-4">
1549+
<a
1550+
:click="parent.activeSection='team'"
1551+
class="cursor-pointer font-bold p-4"
1552+
>
15501553
Team 3
15511554
</a>
15521555
<div class="overflow-hidden">

lib/entity.js

+20-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Interpreter, ClassInterpreter } from './generators/interpreter'
22
import { Lexer } from './generators/lexer'
33
import { Events } from './entity/events'
44
import { Attributes } from './entity/attributes'
5+
import { State } from './state'
56

67
export default class Entity {
78
constructor(el) {
@@ -15,6 +16,8 @@ export default class Entity {
1516
MiniJS.state.addEntity(this)
1617

1718
if (MiniJS.debug) this.element.dataset.entityId = this.id
19+
20+
if (this.element.hasAttribute(':parent')) this.setAsParent()
1821
}
1922

2023
setAsParent() {
@@ -93,11 +96,9 @@ export default class Entity {
9396
this.variables = [...new Set(this.variables)]
9497

9598
this.variables.forEach((variable) => {
96-
if (variable.startsWith('el.') || variable === 'el') {
99+
if (State.isElState(variable)) {
97100
this.setAsParent()
98101

99-
if (!this.parent) this.parent = this.getParent()
100-
101102
if (window[this.id] == null) {
102103
window[this.id] = MiniJS.state.create({}, this.id)
103104
}
@@ -108,6 +109,19 @@ export default class Entity {
108109
const [_, varName] = variable.split('.')
109110
MiniJS.state.addEntityVariable(this.id, varName, this.id)
110111
}
112+
} else if (State.isParentState(variable)) {
113+
if (!this.parent) this.parent = this.getParent()
114+
115+
if (window[this.parent.id] == null) {
116+
window[this.parent.id] = MiniJS.state.create({}, this.parent.id)
117+
}
118+
119+
MiniJS.state.addVariable(this.parent.id, this.id)
120+
121+
if (variable !== 'parent') {
122+
const [_, varName] = variable.split('.')
123+
MiniJS.state.addEntityVariable(this.parent.id, varName, this.id)
124+
}
111125
} else if (typeof window[variable] === 'function') {
112126
this.variables.splice(this.variables.indexOf(variable), 1)
113127
} else {
@@ -130,8 +144,10 @@ export default class Entity {
130144
el: `proxyWindow['${this.id}']`,
131145
}
132146

147+
if (this.parent) ids.parent = `proxyWindow['${this.parent.id}']`
148+
133149
this.variables.forEach((variable) => {
134-
if (variable.startsWith('el.') || variable === 'el') return
150+
if (State.isElState(variable) || State.isParentState(variable)) return
135151

136152
ids[variable] = `proxyWindow-${variable}`
137153
})

lib/state.js

+4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ export class State {
99
return variable.startsWith('el.') || variable === 'el'
1010
}
1111

12+
static isParentState(variable) {
13+
return variable.startsWith('parent.') || variable === 'parent'
14+
}
15+
1216
constructor() {
1317
this.window = null
1418

readme.md

+74-1
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,9 @@ To use variables only in a current event, you can create a local variable using
226226
</button>
227227
```
228228

229-
If you want to use the variable across the element's attributes and events, you can use `el.`:
229+
### Element Variables
230+
231+
If you want to use the variable across an element's attributes and events, you can use `el.`:
230232

231233
```html
232234
<script>
@@ -244,6 +246,77 @@ If you want to use the variable across the element's attributes and events, you
244246

245247
Like the example above, `:load` can be used to set the initial value of the variable.
246248

249+
### Parent Element Variables
250+
251+
Adding a `:parent` attribute to an element will allow you to access its variables from its children using `parent.` variables.
252+
253+
A children's `parent.` variable is the same as the parent's `el.` variable.
254+
255+
```html
256+
<div id="accordion" class="accordion" :parent>
257+
<!-- Parent Element -->
258+
259+
<!-- Children Elements -->
260+
<!-- parent.variable == #accordion's el.variable -->
261+
<section
262+
class="grid transition-all border-gray-300 border border-b-0 rounded hover:bg-gray-100"
263+
>
264+
<button
265+
:click="parent.activeSection = 'about'"
266+
class="cursor-pointer font-bold p-4"
267+
>
268+
About Us
269+
</button>
270+
<div
271+
class="p-4 pt-2 overflow-hidden hidden"
272+
:class="parent.activeSection =='about' ? 'block' : 'hidden'"
273+
>
274+
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy
275+
eirmod.
276+
</div>
277+
</section>
278+
279+
<section
280+
class="grid transition-all border-gray-300 border border-b-0 rounded hover:bg-gray-100"
281+
>
282+
<button
283+
:click="parent.activeSection = 'contact'"
284+
class="cursor-pointer font-bold p-4"
285+
>
286+
Contact Us
287+
</button>
288+
<div
289+
class="p-4 pt-2 overflow-hidden"
290+
:class="parent.activeSection =='contact' ? 'block' : 'hidden'"
291+
>
292+
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy
293+
eirmod.
294+
</div>
295+
</section>
296+
297+
<section
298+
class="grid transition-all border-gray-300 border rounded hover:bg-gray-100"
299+
:class="parent.activeSection =='team' ? 'active' : ''"
300+
>
301+
<button
302+
:click="parent.activeSection = 'team'"
303+
class="cursor-pointer font-bold p-4"
304+
>
305+
Team 3
306+
</button>
307+
<div
308+
class="p-4 pt-2 overflow-hidden"
309+
:class="parent.activeSection =='team' ? 'block' : 'hidden'"
310+
>
311+
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy
312+
eirmod.
313+
</div>
314+
</section>
315+
</div>
316+
```
317+
318+
You
319+
247320
### Variable Methods
248321

249322
MiniJS added some commonly-used custom methods to variables.

0 commit comments

Comments
 (0)