Skip to content

Commit 23aae0e

Browse files
keithamuslangermank
andcommitted
marquee
Co-authored-by: langermank <[email protected]>
1 parent 24b4748 commit 23aae0e

File tree

3 files changed

+66
-30
lines changed

3 files changed

+66
-30
lines changed

examples/index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
<title>custom-element demo</title>
66
</head>
77
<body>
8-
<custom-element></custom-element>
8+
<marquee-text duration="1s"><p>Hello World This is 24 Web Components</p></marquee-text>
99

1010
<script type="module">
1111
// import 'https://unpkg.com/@github/custom-element-boilerplate@latest/dist/custom-element.js'
12-
import '../src/custom-element.ts'
12+
import '../src/marquee-text-element.ts'
1313
</script>
1414
</body>
1515
</html>

src/custom-element.ts

Lines changed: 0 additions & 28 deletions
This file was deleted.

src/marquee-text-element.ts

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
class MarqueeTextElement extends HTMLElement {
2+
static observedAttributes = ['duration']
3+
4+
5+
#renderRoot = this.attachShadow({mode: 'open'})
6+
7+
get duration() {
8+
const value = this.getAttribute('duration')
9+
return value
10+
}
11+
12+
set duration(value: string) {
13+
this.setAttribute('duration', value)
14+
}
15+
16+
attributeChangedCallback(name: 'duration', oldValue: null|string, newValue: null|string) {
17+
if (oldValue === newValue) return
18+
if (newValue === null) return
19+
if (newValue) this.style.setProperty('--animation-duration', newValue)
20+
}
21+
22+
connectedCallback(): void {
23+
this.#renderRoot.innerHTML = `
24+
<style>
25+
@keyframes marqueeeee {
26+
0% {
27+
translate: -100%;
28+
}
29+
100% {
30+
translate: 100vw;
31+
}
32+
}
33+
:host slot {
34+
animation: var(--animation-duration, 5s) linear infinite marqueeeee;
35+
display: inline-block;
36+
}
37+
:host {
38+
overflow: hidden;
39+
max-width: 100vw;
40+
display: block;
41+
}
42+
@media (prefers-reduced-motion) {
43+
:host slot {
44+
animation: none;
45+
}
46+
}
47+
</style>
48+
<slot></slot>
49+
`
50+
}
51+
}
52+
53+
declare global {
54+
interface Window {
55+
MarqueeTextElement: typeof MarqueeTextElement
56+
}
57+
}
58+
59+
export default MarqueeTextElement
60+
61+
if (!window.customElements.get('marquee-text')) {
62+
window.MarqueeTextElement = MarqueeTextElement
63+
window.customElements.define('marquee-text', MarqueeTextElement)
64+
}

0 commit comments

Comments
 (0)