Skip to content

Commit cf8c254

Browse files
committed
docs
1 parent 38584ab commit cf8c254

1 file changed

Lines changed: 235 additions & 1 deletion

File tree

‎packages/docs/src/components/TopBanner.svelte‎

Lines changed: 235 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,102 @@
11
<script>
22
import { page } from "$app/stores"
3+
import { PUBLIC_DAISYUI_API_PATH } from "$env/static/public"
34
import Countdown from "svelte-countdown"
45
import { onMount } from "svelte"
56
const attributes = $derived($page.data.specialDiscount?.data?.attributes)
67
78
let now = $state(Date.now())
9+
let blackFriday = $state(null)
10+
let copyResult = $state(null)
11+
const copyFeedbackDuration = 3000
12+
const blackFridayUrl = `${PUBLIC_DAISYUI_API_PATH}/api/discount_bf.json`
13+
const millisecondsPerSecond = 1000
14+
const secondsPerMinute = 60
15+
const minutesPerHour = 60
16+
17+
function selectDiscount(promotion, timestamp) {
18+
if (
19+
!Array.isArray(promotion?.discounts) ||
20+
!(
21+
Date.parse(promotion.starts_at) <= timestamp && timestamp < Date.parse(promotion.expires_at)
22+
)
23+
)
24+
return null
25+
26+
for (const discount of promotion.discounts) {
27+
if (!discount) continue
28+
const start = Date.parse(discount.starts_at)
29+
const end = Date.parse(discount.expires_at)
30+
if (
31+
typeof discount.code === "string" &&
32+
discount.code.length > 0 &&
33+
Number.isFinite(discount.percentage) &&
34+
discount.percentage > 0 &&
35+
start <= timestamp &&
36+
timestamp < end
37+
) {
38+
const next = promotion.discounts.find(
39+
(candidate) =>
40+
candidate &&
41+
Date.parse(candidate.starts_at) === end &&
42+
Date.parse(candidate.expires_at) > end &&
43+
end < Date.parse(promotion.expires_at) &&
44+
Number.isFinite(candidate.percentage) &&
45+
candidate.percentage > 0 &&
46+
candidate.percentage < discount.percentage,
47+
)
48+
const previous = promotion.discounts.find(
49+
(candidate) =>
50+
candidate &&
51+
Date.parse(candidate.expires_at) === start &&
52+
Date.parse(candidate.starts_at) < start &&
53+
Number.isFinite(candidate.percentage) &&
54+
candidate.percentage > discount.percentage,
55+
)
56+
return { discount, previous, next, start, end }
57+
}
58+
}
59+
return null
60+
}
61+
62+
const activeDiscount = $derived(selectDiscount(blackFriday, now))
63+
const copyFeedback = $derived(
64+
copyResult?.code === activeDiscount?.discount.code && now < copyResult?.until
65+
? copyResult
66+
: null,
67+
)
68+
const secondsLeft = $derived(
69+
activeDiscount ? Math.max(0, Math.ceil((activeDiscount.end - now) / millisecondsPerSecond)) : 0,
70+
)
71+
const hoursLeft = $derived(Math.floor(secondsLeft / (secondsPerMinute * minutesPerHour)))
72+
const minutesLeft = $derived(Math.floor(secondsLeft / secondsPerMinute) % minutesPerHour)
73+
const remainingSeconds = $derived(secondsLeft % secondsPerMinute)
74+
const elapsedDay = $derived(
75+
activeDiscount
76+
? Math.min(
77+
1,
78+
Math.max(0, (now - activeDiscount.start) / (activeDiscount.end - activeDiscount.start)),
79+
)
80+
: 0,
81+
)
882
983
onMount(() => {
84+
const controller = new AbortController()
85+
fetch(blackFridayUrl, { signal: controller.signal })
86+
.then((response) => (response.ok ? response.json() : null))
87+
.then((promotion) => {
88+
if (!controller.signal.aborted) blackFriday = promotion
89+
})
90+
.catch(() => {
91+
// This optional banner stays hidden if the API is unavailable.
92+
})
1093
const timer = setInterval(() => {
1194
now = Date.now()
1295
}, 1000)
13-
return () => clearInterval(timer)
96+
return () => {
97+
clearInterval(timer)
98+
controller.abort()
99+
}
14100
})
15101
16102
const dateFormat = {
@@ -23,6 +109,154 @@
23109
}
24110
</script>
25111
112+
<!-- decremental -->
113+
{#if true && activeDiscount}
114+
<section
115+
dir="ltr"
116+
aria-label="Black Friday daily discounts in UTC"
117+
class="bg-neutral text-neutral-content border-neutral-content/10 border-b px-4 py-2 font-mono text-xs sm:px-6"
118+
>
119+
<div
120+
class="mx-auto flex max-w-2xl flex-col gap-3 sm:flex-row sm:items-center sm:justify-between sm:gap-6"
121+
>
122+
<div class="min-w-0 flex flex-col gap-1">
123+
<div class="font-title text-base font-bold uppercase">
124+
Black Friday discount
125+
</div>
126+
<div class="text-[0.6875rem] opacity-60 mb-3">
127+
For daisyUI
128+
<a href="/blueprint/" class="link underline-offset-3">MCP</a>,
129+
<a href="/store/daisyui-figma-library/" class="link underline-offset-3">Figma</a> and
130+
<a href="/store/" class="link underline-offset-3">templates</a>
131+
</div>
132+
<div class="flex flex-wrap items-center gap-x-1.5 gap-y-1 text-[0.6875rem]">
133+
<span>Use code</span>
134+
<div class="tooltip tooltip-error" class:tooltip-open={copyFeedback?.success === true}>
135+
<div class="tooltip-content text-xs">
136+
{copyFeedback?.success ? "copied" : "copy"}
137+
</div>
138+
<button
139+
type="button"
140+
class="btn btn-xs border-neutral-content/25 bg-neutral-content/10 text-neutral-content hover:border-neutral-content/50 hover:bg-neutral-content/20 shadow-none"
141+
aria-label={`Copy discount code ${activeDiscount.discount.code}`}
142+
onclick={async () => {
143+
const code = activeDiscount.discount.code
144+
try {
145+
await navigator.clipboard.writeText(code)
146+
copyResult = { code, success: true, until: Date.now() + copyFeedbackDuration }
147+
} catch {
148+
copyResult = { code, success: false, until: Date.now() + copyFeedbackDuration }
149+
}
150+
}}
151+
>
152+
<code class="font-mono text-xs font-normal tracking-wide select-all"
153+
>{activeDiscount.discount.code}</code
154+
>
155+
<svg
156+
aria-hidden="true"
157+
class="size-3.5 shrink-0"
158+
xmlns="http://www.w3.org/2000/svg"
159+
viewBox="0 0 24 24"
160+
fill="none"
161+
stroke="currentColor"
162+
stroke-width="1.5"
163+
stroke-linecap="round"
164+
stroke-linejoin="round"
165+
>
166+
{#if copyFeedback?.success}
167+
<path d="m5 12 4 4L19 6" />
168+
{:else}
169+
<rect x="8" y="8" width="12" height="12" rx="2" />
170+
<path d="M16 8V4a2 2 0 0 0-2-2H4a2 2 0 0 0-2 2v10a2 2 0 0 0 2 2h4" />
171+
{/if}
172+
</svg>
173+
</button>
174+
</div>
175+
<span class="inline-flex items-center gap-1.5 whitespace-nowrap">
176+
Expires in
177+
<span
178+
class="countdown text-error font-mono text-xs font-semibold tabular-nums"
179+
role="timer"
180+
aria-live="off"
181+
aria-label={`${hoursLeft} hours, ${minutesLeft} minutes, ${remainingSeconds} seconds left for today's discount`}
182+
dir="ltr"
183+
title={`Ends ${new Date(activeDiscount.end).toISOString().slice(0, 16).replace("T", " ")} UTC`}
184+
>
185+
<span aria-hidden="true" style={`--value:${hoursLeft}; --digits:2;`}>{hoursLeft}</span
186+
>:<span aria-hidden="true" style={`--value:${minutesLeft}; --digits:2;`}
187+
>{minutesLeft}</span
188+
>:<span aria-hidden="true" style={`--value:${remainingSeconds}; --digits:2;`}
189+
>{remainingSeconds}</span
190+
>
191+
</span>
192+
</span>
193+
</div>
194+
<span
195+
role="status"
196+
class="block text-[0.625rem]"
197+
class:sr-only={copyFeedback?.success !== false}
198+
>
199+
{#if copyFeedback?.success}Copied!{:else if copyFeedback?.success === false}Copy failed.
200+
Select the code to copy it.{/if}
201+
</span>
202+
</div>
203+
<div
204+
class="relative h-16 w-full max-w-80 shrink-0 self-center"
205+
dir="ltr"
206+
aria-label="Daily discount time indicator (UTC)"
207+
>
208+
<div
209+
class="absolute inset-x-0 top-5 h-1 overflow-hidden [mask-image:linear-gradient(to_right,transparent,black_12%,black_88%,transparent)]"
210+
aria-hidden="true"
211+
>
212+
<progress
213+
class="progress text-neutral-content absolute -left-1/2 block h-1 w-[200%] rounded-none"
214+
value={3 + elapsedDay}
215+
max="6"
216+
></progress>
217+
</div>
218+
{#if activeDiscount.previous}
219+
<div
220+
class="text-neutral-content/55 absolute start-1/6 top-0 flex -translate-x-1/2 flex-col items-center text-[0.625rem]"
221+
>
222+
<span>Yesterday</span>
223+
<span
224+
class="bg-neutral border-neutral-content my-2.75 h-2 w-px border-s"
225+
aria-hidden="true"
226+
></span>
227+
<span class="text-sm leading-none tabular-nums line-through"
228+
>{activeDiscount.previous ? `${activeDiscount.previous.percentage}%` : ""}</span
229+
>
230+
</div>
231+
{/if}
232+
<div
233+
class="absolute start-1/2 top-0 flex -translate-x-1/2 flex-col items-center text-[0.625rem]"
234+
>
235+
<span class="font-bold">Today</span>
236+
<span class="bg-neutral-content my-2.75 h-2 w-0.5" aria-hidden="true"></span>
237+
<span class="text-lg leading-none font-semibold font-title tabular-nums text-error"
238+
>{activeDiscount.discount.percentage}%</span
239+
>
240+
</div>
241+
{#if activeDiscount.next}
242+
<div
243+
class="text-neutral-content/65 absolute start-5/6 top-0 flex -translate-x-1/2 flex-col items-center text-[0.625rem]"
244+
>
245+
<span>Tomorrow</span>
246+
<span
247+
class="bg-neutral border-neutral-content/30 my-2.75 h-2 w-px border-s"
248+
aria-hidden="true"
249+
></span>
250+
<span class="text-sm leading-none tabular-nums"
251+
>{activeDiscount.next ? `${activeDiscount.next.percentage}%` : "—"}</span
252+
>
253+
</div>
254+
{/if}
255+
</div>
256+
</div>
257+
</section>
258+
{/if}
259+
26260
<!-- Without timer -->
27261
{#if false && !$page.url.pathname.startsWith("/blueprint/")}
28262
<div class="bg-base-100 flex justify-center rounded-sm">

0 commit comments

Comments
 (0)