-
Notifications
You must be signed in to change notification settings - Fork 79
Implement custom elements interface for Crank components #307
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
brainkim
wants to merge
3
commits into
main
Choose a base branch
from
custom-elements
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,276 @@ | ||
| import {renderer} from "@b9g/crank/dom"; | ||
| import {createCustomElementClass} from "@b9g/crank/custom-elements"; | ||
|
|
||
| // Revive the classic <blink> element! | ||
| function* Blink({rate = 1000, children, ref}) { | ||
| let visible = true; | ||
|
|
||
| ref?.((element) => ({ | ||
| show() { | ||
| element.setAttribute("visible", "true"); | ||
| }, | ||
| hide() { | ||
| element.setAttribute("visible", "false"); | ||
| }, | ||
| toggle() { | ||
| const current = element.getAttribute("visible"); | ||
| element.setAttribute("visible", current === "false" ? "true" : "false"); | ||
| }, | ||
| onblink: null, | ||
| })); | ||
|
|
||
| const interval = setInterval(() => { | ||
| this.refresh(() => { | ||
| visible = !visible; | ||
| this.setAttribute("visible", visible.toString()); | ||
| // Dispatch event in this.after to prevent re-entrancy | ||
| this.after(() => { | ||
| this.dispatchEvent( | ||
| new CustomEvent("blink", { | ||
| detail: {visible}, | ||
| bubbles: true, | ||
| }), | ||
| ); | ||
| }); | ||
| }); | ||
| }, rate); | ||
|
|
||
| try { | ||
| for ({rate: rateAttr, children} of this) { | ||
| // Handle rate as string from attributes | ||
| rate = parseFloat(rateAttr) || 1000; | ||
| const isVisible = this.getAttribute("visible") !== "false"; | ||
|
|
||
| yield ( | ||
| <span | ||
| style={{ | ||
| visibility: isVisible ? "visible" : "hidden", | ||
| }} | ||
| > | ||
| {children} | ||
| </span> | ||
| ); | ||
| } | ||
| } finally { | ||
| clearInterval(interval); | ||
| } | ||
| } | ||
|
|
||
| // Revive the classic <marquee> element! | ||
| function* Marquee({speed = 50, children, ref}) { | ||
| let position = 0; | ||
| let containerWidth = 0; | ||
| let contentWidth = 0; | ||
|
|
||
| ref?.((element) => ({ | ||
| start() { | ||
| element.play(); | ||
| }, | ||
| stop() { | ||
| element.pause(); | ||
| }, | ||
| play() { | ||
| element.setAttribute("playing", "true"); | ||
| }, | ||
| pause() { | ||
| element.removeAttribute("playing"); | ||
| }, | ||
| onmarqueebounce: null, | ||
| })); | ||
|
|
||
| this.after((el) => { | ||
| const container = el.querySelector(".marquee-container"); | ||
| const content = el.querySelector(".marquee-content"); | ||
| containerWidth = container.offsetWidth; | ||
| contentWidth = content.offsetWidth; | ||
| }); | ||
|
|
||
| let animationId; | ||
| const animate = () => { | ||
| if (this.getAttribute("playing") === "true") { | ||
| this.refresh(() => { | ||
| position += speed / 60; // Adjust for ~60fps | ||
|
|
||
| // Bounce effect | ||
| if (position > containerWidth) { | ||
| position = -contentWidth; | ||
| // Dispatch event in this.after to prevent re-entrancy | ||
| this.after(() => { | ||
| this.dispatchEvent( | ||
| new CustomEvent("marqueebounce", { | ||
| detail: {position}, | ||
| bubbles: true, | ||
| }), | ||
| ); | ||
| }); | ||
| } | ||
| }); | ||
| } | ||
| animationId = requestAnimationFrame(animate); | ||
| }; | ||
| animationId = requestAnimationFrame(animate); | ||
|
|
||
| try { | ||
| for ({speed: speedAttr, children} of this) { | ||
| // Handle speed as string from attributes | ||
| speed = parseFloat(speedAttr) || 50; | ||
|
|
||
| yield ( | ||
| <div | ||
| class="marquee-container" | ||
| style={{ | ||
| position: "relative", | ||
| overflow: "hidden", | ||
| width: "100%", | ||
| height: "30px", | ||
| }} | ||
| > | ||
| <div | ||
| class="marquee-content" | ||
| style={{ | ||
| position: "absolute", | ||
| left: `${position}px`, | ||
| whiteSpace: "nowrap", | ||
| }} | ||
| > | ||
| {children} | ||
| </div> | ||
| </div> | ||
| ); | ||
| } | ||
| } finally { | ||
| cancelAnimationFrame(animationId); | ||
| } | ||
| } | ||
|
|
||
| // Create custom element classes | ||
| const BlinkElement = createCustomElementClass(Blink, { | ||
| observedAttributes: ["rate", "visible"], | ||
| }); | ||
|
|
||
| const MarqueeElement = createCustomElementClass(Marquee, { | ||
| observedAttributes: ["speed", "playing"], | ||
| }); | ||
|
|
||
| // Register custom elements | ||
| customElements.define("rip-blink", BlinkElement); | ||
| customElements.define("rip-marquee", MarqueeElement); | ||
|
|
||
| // Demo app | ||
| const style = ` | ||
| body { | ||
| font-family: Arial, sans-serif; | ||
| max-width: 800px; | ||
| margin: 0 auto; | ||
| padding: 20px; | ||
| } | ||
| .demo-section { | ||
| margin: 30px 0; | ||
| padding: 20px; | ||
| border: 1px solid #ddd; | ||
| border-radius: 8px; | ||
| } | ||
| .controls { | ||
| margin-top: 10px; | ||
| } | ||
| button { | ||
| margin-right: 10px; | ||
| padding: 5px 15px; | ||
| cursor: pointer; | ||
| } | ||
| rip-blink { | ||
| font-weight: bold; | ||
| color: red; | ||
| } | ||
| rip-marquee { | ||
| display: block; | ||
| background: #f0f0f0; | ||
| padding: 5px; | ||
| margin: 10px 0; | ||
| } | ||
| `; | ||
|
|
||
| document.addEventListener("DOMContentLoaded", () => { | ||
| renderer.render( | ||
| <div> | ||
| <style>{style}</style> | ||
| <h1>Retro Web Elements with Crank Custom Elements!</h1> | ||
|
|
||
| <div class="demo-section"> | ||
| <h2>The Infamous Blink</h2> | ||
| <p> | ||
| Remember when <rip-blink>EVERYTHING BLINKED</rip-blink> on the web? | ||
| Now you can make <rip-blink rate="500">anything blink</rip-blink>{" "} | ||
| again! | ||
| </p> | ||
|
|
||
| <p> | ||
| <rip-blink rate="2000" visible="true"> | ||
| This blinks slowly and thoughtfully | ||
| </rip-blink> | ||
| </p> | ||
|
|
||
| <div class="controls"> | ||
| <button onclick="document.querySelectorAll('rip-blink').forEach(b => b.toggle())"> | ||
| Toggle All Blinks | ||
| </button> | ||
| <button onclick="document.querySelectorAll('rip-blink').forEach(b => b.show())"> | ||
| Show All | ||
| </button> | ||
| <button onclick="document.querySelectorAll('rip-blink').forEach(b => b.hide())"> | ||
| Hide All | ||
| </button> | ||
| </div> | ||
| </div> | ||
|
|
||
| <div class="demo-section"> | ||
| <h2>The Glorious Marquee</h2> | ||
| <rip-marquee speed="30" playing="true"> | ||
| 🎉 Breaking News: The 90s are back! Marquee elements are cool again! | ||
| 🎉 | ||
| </rip-marquee> | ||
|
|
||
| <rip-marquee speed="60"> | ||
| <span style="color: blue">This marquee is faster! </span> | ||
| <span style="color: red">And more colorful! </span> | ||
| <span style="color: green">Click play to start! </span> | ||
| </rip-marquee> | ||
|
|
||
| <div class="controls"> | ||
| <button onclick="document.querySelectorAll('rip-marquee').forEach(m => m.play())"> | ||
| Play All | ||
| </button> | ||
| <button onclick="document.querySelectorAll('rip-marquee').forEach(m => m.pause())"> | ||
| Pause All | ||
| </button> | ||
| </div> | ||
| </div> | ||
|
|
||
| <div class="demo-section"> | ||
| <h2>Event Playground</h2> | ||
| <p>Open the console to see custom events!</p> | ||
| <rip-blink id="event-blink"> | ||
| Blink events fire on each toggle! | ||
| </rip-blink> | ||
| <rip-marquee id="event-marquee" speed="80" playing="true"> | ||
| Watch for bounce events! → | ||
| </rip-marquee> | ||
| </div> | ||
| </div>, | ||
| document.body, | ||
| ); | ||
|
|
||
| // Set up event listeners | ||
| const eventBlink = document.getElementById("event-blink"); | ||
| const eventMarquee = document.getElementById("event-marquee"); | ||
|
|
||
| eventBlink.onblink = (e) => { | ||
| // eslint-disable-next-line no-console | ||
| console.log("Blink toggled! Visible:", e.detail.visible); | ||
| }; | ||
|
|
||
| eventMarquee.onmarqueebounce = (e) => { | ||
| // eslint-disable-next-line no-console | ||
| console.log("Marquee bounced!", e.detail); | ||
| }; | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we do host styles.