Skip to content

Commit

Permalink
Merge pull request #934 from AlejandroSuero/feature/refactor-dom-sele…
Browse files Browse the repository at this point in the history
…ctors

feat: refactor DOM selectors, using `@/lib/dom-selector`
  • Loading branch information
midudev authored Apr 29, 2024
2 parents a052688 + f05264e commit 15f5b8d
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 28 deletions.
8 changes: 5 additions & 3 deletions src/components/AnimatedX.astro
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@
>
</span>
<script>
import { $ } from "@/lib/dom-selector"

let intervalId: NodeJS.Timeout
type AnimatedXElement = SVGGElement | null

Expand All @@ -152,9 +154,9 @@
if (prefersReducedMotion) return

intervalId && clearInterval(intervalId)
const $svgGroup1 = document.getElementById("layer1") as AnimatedXElement
const $svgGroup2 = document.getElementById("layer2") as AnimatedXElement
const $svgGroup3 = document.getElementById("layer3") as AnimatedXElement
const $svgGroup1 = $("#layer1") as AnimatedXElement
const $svgGroup2 = $("#layer2") as AnimatedXElement
const $svgGroup3 = $("#layer3") as AnimatedXElement

intervalId = setInterval(() => drawGroup([$svgGroup1, $svgGroup2, $svgGroup3]), INTERVAL_TIME)
})
Expand Down
8 changes: 5 additions & 3 deletions src/components/Boxers/Gallery.astro
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,11 @@ const { id, name } = Astro.props
</style>

<script>
import { $ } from "@/lib/dom-selector"

document.addEventListener("astro:page-load", () => {
const $lightbox = document.getElementById("lightbox") as HTMLDialogElement
const $gallery = document.getElementById("boxer-gallery") as HTMLElement
const $lightbox = $("#lightbox") as HTMLDialogElement
const $gallery = $("#boxer-gallery") as HTMLElement
if (!$gallery) return

const $links = $gallery?.querySelectorAll("a")
Expand Down Expand Up @@ -167,7 +169,7 @@ const { id, name } = Astro.props
$close?.addEventListener("click", close)

$lightbox?.addEventListener("click", (event) => {
const $dialogBg = document.querySelector("[data-dialog-bg]") as HTMLDivElement
const $dialogBg = $("[data-dialog-bg]") as HTMLDivElement
const canClose = event.target === $dialogBg || event.target === $lightbox
if (canClose) close()
})
Expand Down
4 changes: 3 additions & 1 deletion src/components/ButtonUp.astro
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@
</div>

<script>
import { $ } from "@/lib/dom-selector"

document.addEventListener("astro:page-load", () => {
let timeout: number = 0 // Identificador para clearTimeout
const $button = document.getElementById("scroll-to-top") as HTMLButtonElement
const $button = $("#scroll-to-top") as HTMLButtonElement

const DISPLAY = { BLOCK: "block", NONE: "none" } as const

Expand Down
4 changes: 3 additions & 1 deletion src/components/CalendarButton.astro
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import Action from "@/components/Action.astro"
</Action>

<script is:inline>
import { $ } from "@/lib/dom-selector"

document.addEventListener("astro:page-load", () => {
const config = {
name: "🥊 La Velada del Año 4 - El Evento del Año",
Expand Down Expand Up @@ -42,7 +44,7 @@ import Action from "@/components/Action.astro"
})
}

const button = document.querySelector("#add-to-calendar")
const button = $("#add-to-calendar")

const handleClick = async () => {
if (status === "loading") return
Expand Down
10 changes: 6 additions & 4 deletions src/components/ClipsModal.astro
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,13 @@
</style>

<script>
import { $, $$ } from "@/lib/dom-selector"

document.addEventListener("astro:page-load", () => {
const $clipContainer = document.querySelectorAll(".clip-container")
const $clipDialog = document.querySelector(".clip-dialog") as HTMLDialogElement
const $closeButton = document.querySelector(".close-dialog")
const $ytFrame = document.querySelector(".yt-iframe")
const $clipContainer = $$(".clip-container")
const $clipDialog = $(".clip-dialog") as HTMLDialogElement
const $closeButton = $(".close-dialog")
const $ytFrame = $(".yt-iframe")

if (!$clipDialog) return

Expand Down
4 changes: 3 additions & 1 deletion src/components/SmokeBackground.astro
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
</div>

<script>
import { $ } from "@/lib/dom-selector"

const NUM_PARTICLES = 50 // Número de partículas
const canvas = document.getElementById("smoke-canvas") as HTMLCanvasElement
const canvas = $("#smoke-canvas") as HTMLCanvasElement
const ctx = canvas.getContext("2d") as CanvasRenderingContext2D
let raf: number

Expand Down
14 changes: 8 additions & 6 deletions src/components/TwitchLiveBox.astro
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,22 @@ const { streamer = "ibai" } = Astro.props
</div>

<script is:inline>
import { $ } from "@/lib/dom-selector"

const CONFIG = {
MIDU_API_UPTIME_WORKER: "https://midudev-apis.midudev.workers.dev/uptime",
VELADA_DATE: "2024-07-13 19:00:00 GMT+0100",
TWITCH_PLAYER_URL: "https://player.twitch.tv",
}
const streamer = document.querySelector("#streamer").value
const streamer = $("#streamer").value
const isVeladaTime = new Date().getTime() >= new Date(CONFIG.VELADA_DATE).getTime()
if (isVeladaTime) {
fetch(`${CONFIG.MIDU_API_UPTIME_WORKER}/${streamer}`)
.then((res) => res.json())
.then(({ online }) => {
if (online) {
document.querySelector("#live").style.display = "block"
document.querySelector("#live-frame").innerHTML =
$("#live").style.display = "block"
$("#live-frame").innerHTML =
`<iframe src="${CONFIG.TWITCH_PLAYER_URL}/?channel=${streamer}&parent=${document.location.hostname}" frameborder="0" width="380" height="200" allowfullscreen="true" scrolling="no"></iframe>`
}
})
Expand All @@ -62,9 +64,9 @@ const { streamer = "ibai" } = Astro.props
})
}

const closeButton = document.querySelector("#live-close")
const closeButton = $("#live-close")
closeButton?.addEventListener("click", () => {
document.querySelector("#live").style.display = "none"
document.querySelector("#live").innerHTML = ""
$("#live").style.display = "none"
$("#live").innerHTML = ""
})
</script>
15 changes: 8 additions & 7 deletions src/function/toast.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// from: https://github.com/dgtlss/butterup/tree/main
import { $ } from "@/lib/dom-selector"

interface ToastOptions {
title: string
Expand Down Expand Up @@ -41,7 +42,7 @@ export const butterup: ButterupProps = {
stackedToasts: true,
},
toast({ title, message, type, location, icon, theme, dismissible }) {
let toaster = document.getElementById("toaster")
let toaster = $("#toaster")
if (toaster == null) {
// toaster doesn't exist, create it
toaster = document.createElement("div")
Expand All @@ -54,7 +55,7 @@ export const butterup: ButterupProps = {
document.body.appendChild(toaster)

// Create the toasting rack inside of the toaster
if (document.getElementById("butterupRack") == null) {
if ($("#butterupRack") == null) {
const rack = document.createElement("ol")
rack.id = "butterupRack"
rack.className = "rack"
Expand Down Expand Up @@ -85,8 +86,8 @@ export const butterup: ButterupProps = {
// Check if there are too many toasts on the screen
if (butterup.options.currentToasts >= butterup.options.maxToasts) {
// there are too many toasts on the screen, delete the oldest one
const oldestToast = document.getElementById("butterupRack")?.firstChild
if (oldestToast) document.getElementById("butterupRack")?.removeChild(oldestToast)
const oldestToast = $("#butterupRack")?.firstChild
if (oldestToast) $("#butterupRack")?.removeChild(oldestToast)
butterup.options.currentToasts--
}

Expand Down Expand Up @@ -119,7 +120,7 @@ export const butterup: ButterupProps = {
}

// Add the toast to the rack
document.getElementById("butterupRack")?.appendChild(toast)
$("#butterupRack")?.appendChild(toast)

// check if the user wants an icon
if (icon != null && icon === true) {
Expand Down Expand Up @@ -205,7 +206,7 @@ export const butterup: ButterupProps = {
},
despawnToast(toastId) {
// fade out the toast and then remove it from the DOM
const toast = document.getElementById(toastId)
const toast = $(`#${toastId}`)
// does the toast exist?
if (toast != null) {
toast.className += " fadeOutToast"
Expand All @@ -220,7 +221,7 @@ export const butterup: ButterupProps = {
}
// if this was the last toast on the screen, remove the toaster
if (butterup.options.currentToasts === 0) {
const toaster = document.getElementById("toaster")
const toaster = $("#toaster")
if (toaster) toaster.parentNode?.removeChild(toaster)
}
}, 500)
Expand Down
2 changes: 1 addition & 1 deletion src/sections/PrincipalDate.astro
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ import MapMarkerIcon from "@/icons/map-marker.astro"
// Verifica si el evento ya ha pasado
const eventHasPassed = EVENT_TIMESTAMP < Date.now()
if (eventHasPassed) {
const addToCalendarBtn = document.getElementById("add-to-calendar")
const addToCalendarBtn = $("#add-to-calendar")
if (addToCalendarBtn) {
addToCalendarBtn.remove()
}
Expand Down
2 changes: 1 addition & 1 deletion src/sections/SelectYourBoxer.astro
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ const boxerColumns = [
new URLSearchParams(window.location.search).get("boxer") ||
"el-mariana"

const updatedSelectedBoxerElement: HTMLElement | null = document.querySelector(
const updatedSelectedBoxerElement: HTMLElement | null = $(
`.boxer-link[data-id=${updatedSelectedBoxerId}]`
)
if (updatedSelectedBoxerElement) {
Expand Down

0 comments on commit 15f5b8d

Please sign in to comment.