-
Notifications
You must be signed in to change notification settings - Fork 0
/
parks.html
133 lines (128 loc) · 3.93 KB
/
parks.html
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>parks</title>
<meta name="viewport" content="width=device-width, minimum-scale=1, initial-scale=1, user-scalable=yes">
<style>
html{background:#def;}
body {margin:0;padding:3rem;font-size:1rem;font-family:system, system-ui, -apple-system, ".SFNSText-Regular", "San Francisco", "Oxygen", "Ubuntu", "Roboto", "Segoe UI", "Helvetica Neue", "Lucida Grande", sans-serif; line-height: 1.3; background-color:#fff;color:#000;min-height:100vh;min-width:100vw;box-sizing:border-box;}
us-park{background-color:#fff;position:relative;display:block;}
us-park:hover,
us-park:focus-within{box-shadow:0 0 5px #ddd}
us-park img{border:1px solid #000;width:50px;transition:all 1s;float:left;margin-block-end:0.3em;margin-inline-end:0.5em;}
us-park img:hover:not(:focus){animation:450ms linear 0s 1 tip;}
us-park img:focus{width:200px;}
us-park p{margin:0;}
us-park a[img]{position:absolute;top:-1em;right:3em;background#fff;text-decoration:none;display:block;padding:0.5em;background:#fff;box-shadow:0 0 5px #ddd;transition:opacity 0.5s;opacity:0;}
us-park a[img]:before{content:'📷';}
us-park a[img]:focus,
us-park a[img]:hover{box-shadow: 0 0 5px #aaa;}
us-park:hover a[img],
us-park:focus-within a[img]{opacity:1;}
@keyframes tip{ 0% {transform:rotate(0deg);} 25% {transform:rotate(3deg);} 75% {transform:rotate(-3deg);} 100% {transform:rotate(0deg);} }
</style>
</head>
<body>
<us-national-parks src="./parks.json"></us-national-parks>
<script>
customElements.define('us-park', class USPark extends HTMLElement{
constructor(){
super();
this.data = {};
this.attachShadow({mode: 'open', delegatesFocus: true}).innerHTML = `
<style>
:host{margin-block-end:1em;padding:0.5em;max-width:50em;display:block;}
:host(:focus-within){}
</style>
<slot></slot>
`;
}
});
customElements.define('us-national-parks', class NationParks extends HTMLElement{
constructor(){
super();
this.data = [];
this.update = this.update.bind(this);
this.render = this.render.bind(this);
this.attachShadow({mode: 'open'}).innerHTML = `
<style>
:host(:not([hidden])){
display:flex;
flex-direction: column;
}
[error]{color:red;}
[error]:empty{display:none;}
</style>
<div error></div>
<slot></slot>
`;
this.error = this.shadowRoot.querySelector('[error]');
this.shadowRoot.addEventListener('focusin', this.locate);
}
locate(e){
const park = e.composedPath().find(node=>node.matches && node.matches('us-park'));
if(park){
location.hash = park.getAttribute('name') || '';
}
}
update(res){
if(!res.ok){
console.error(res);
return this.error.textContent = `error: ${ res.statusText } ${ res.status }`;
}
return res.json()
.then(this.render)
.catch(err=>{
console.error(err);
this.error.textContent = `error: ${err}`;
return err;
})
;
}
attributeChangedCallback(name, old, value){
switch(name){
case 'src':
if(old !== value && value){
fetch(value).then(this.update);
};
break;
}
}
get src(){
return this.getAttribute('src');
}
set src(url){
this.setAttribute('src', url);
}
item(data, i){
return `
<us-park index=${i} name="${ encodeURIComponent( data.Name ) }">
<img src="${ data.Thumbnail }" alt="${ data.Name }, a park in ${ data.Location }" tabindex=0>
<p>
<b>${ data.Name }</b> ${ data.Location }, established ${ data.Established }
<br>${ data.Area }
<br>${ data['Recreation visitors'] }
<br>${ data.Description }
<a img title="large image" href="${ data.Image }" target="_blank" tabindex=-1> </a>
</p>
</us-park>`;
}
render(data=this.data){
this.data = data;
cancelAnimationFrame(this._render);
this._render = requestAnimationFrame(()=>{
this.error.textContent = '';
this.innerHTML = data.map(this.item, this).join('');
const active = this.querySelector( `[name="${ location.hash.slice(1) }"]` );
if(!active) return;
active.scrollIntoViewIfNeeded();
active.focus();
});
}
static get observedAttributes(){
return ['src'];
}
});
</script>
</body></html>