-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathDynamicHead.njs
More file actions
96 lines (89 loc) · 3.28 KB
/
DynamicHead.njs
File metadata and controls
96 lines (89 loc) · 3.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/* eslint-disable nullstack/self-closing-comp */
import Nullstack from 'nullstack'
class DynamicHead extends Nullstack {
count = 0
negativeCount = 0
renderHead() {
const innerComponent = `[data-inner-component] { color: blue }`
return <style html={innerComponent} data-inner-component />
}
checkScriptChildren({ element }) {
this.scriptIsEmpty = !element.innerText
}
render() {
const color = this.count % 2 === 0 ? 'red' : 'blue'
const redBlue = `[data-red-blue] { color: ${color}}`
const prerenderConditional = `[data-prerender-conditional] { color: blue }`
const rerenderConditional = `[data-rerender-conditional] { color: blue }`
const fragment = `[data-fragment] { color: blue }`
const conditionalHead = `[data-conditional-head] { color: blue }`
const dynamicLength = `[data-dynamic-length] { color: blue }`
return (
<div data-hydrated={this.hydrated}>
<head>
<script data-script-is-empty={this.scriptIsEmpty} ref={this.checkScriptChildren}></script>
<style html={redBlue} data-count={this.count} data-red-blue />
{this.count === 0 && <style html={prerenderConditional} data-prerender-conditional />}
{this.count === 1 && <style html={rerenderConditional} data-rerender-conditional />}
<>
<style html={fragment} data-fragment />
</>
<Head />
</head>
{this.count === 1 && (
<head>
<style html={conditionalHead} data-conditional-head />
</head>
)}
<head>
{this.count % 2 === 0 ? (
<style html="" data-ternary-head-children />
) : (
<meta name="test" content="nullstack" data-ternary-head-children />
)}
</head>
<head>
{Array(this.count + 1 - this.negativeCount).fill(
<style
html={dynamicLength}
data-dynamic-length={this.count}
data-negative-count={this.negativeCount === 1}
/>,
)}
</head>
<button onclick={{ count: this.count + 1 }} data-increment>
{' '}
inc {this.count}{' '}
</button>
<button onclick={{ negativeCount: this.negativeCount + 1 }} data-decrement>
{' '}
dec {this.negativeCount}{' '}
</button>
<span data-red-blue> data-red-blue </span>
<span data-prerender-conditional> data-prerender-conditional </span>
<span data-rerender-conditional> data-rerender-conditional </span>
<span data-fragment> data-fragment </span>
<span data-conditional-head> data-conditional-head </span>
<span data-inner-component> data-inner-component </span>
{this.count % 2 === 0 ? (
<head>
<meta name="test" content="nullstack" data-ternary-head id="ternary-head" />
</head>
) : (
<span data-ternary-span> not head </span>
)}
{this.count % 2 === 0 ? (
<head>
<meta name="test" content="nullstack" data-a1 />
</head>
) : (
<head>
<meta name="test" content="nullstack" data-b1 />
<meta name="test" content="nullstack" data-b2 />
</head>
)}
</div>
)
}
}
export default DynamicHead