Skip to content
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

Add Glow component for creating glowing effects #300

Merged
merged 1 commit into from
Mar 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 110 additions & 0 deletions src/components/Glow.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
---
import type { HTMLTag, Polymorphic } from "astro/types"

type Props<Tag extends HTMLTag> = Polymorphic<{
as: Tag
}>

const { as: Tag, class: className, ...props } = Astro.props
---

<Tag data-glow class:list={["glow-box", className]} {...props}>
<div data-glow></div><slot />
</Tag>

<script>
const syncPointer = ({ x, y }) => {
document.documentElement.style.setProperty("--x", x.toFixed(2))
document.documentElement.style.setProperty("--xp", (x / window.innerWidth).toFixed(2))
document.documentElement.style.setProperty("--y", y.toFixed(2))
document.documentElement.style.setProperty("--yp", (y / window.innerHeight).toFixed(2))
}
document.body.addEventListener("pointermove", syncPointer)
</script>
<style>
:root {
--backdrop: hsl(0 0% 60% / 0.12);
--border: 2;
--backup-border: var(--backdrop);
--size: 100;
}
.glow-box {
position: relative;
box-shadow: 0 1rem 2rem -1rem black;
backdrop-filter: blur(calc(var(--cardblur, 5) * 1px));
}
[data-glow] {
--border-size: calc(var(--border, 2) * 1px);
--spotlight-size: calc(var(--size, 150) * 1px);
--hue: calc(var(--base) + (var(--xp, 0) * var(--spread, 0)));
background-image: radial-gradient(
var(--spotlight-size) var(--spotlight-size) at calc(var(--x, 0) * 1px) calc(var(--y, 0) * 1px),
hsl(
var(--hue, 70) calc(var(--saturation, 100) * 1%) calc(var(--lightness, 50) * 1%) /
var(--bg-spot-opacity, 0.1)
),
transparent
);
background-color: var(--backdrop, transparent);
background-size: calc(100% + (2 * var(--border-size))) calc(100% + (2 * var(--border-size)));
background-position: 50% 50%;
background-attachment: fixed;
border: var(--border-size) solid var(--backup-border);
position: relative;
touch-action: none;
}
[data-glow]::before,
[data-glow]::after {
pointer-events: none;
content: "";
position: absolute;
inset: calc(var(--border-size) * -1);
border: var(--border-size) solid transparent;
background-attachment: fixed;
background-size: calc(100% + (2 * var(--border-size))) calc(100% + (2 * var(--border-size)));
background-repeat: no-repeat;
background-position: 50% 50%;
mask: linear-gradient(transparent, transparent), linear-gradient(white, white);
mask-clip: padding-box, border-box;
mask-composite: intersect;
}
[data-glow]::before {
background-image: radial-gradient(
calc(var(--spotlight-size) * 0.75) calc(var(--spotlight-size) * 0.75) at
calc(var(--x, 0) * 1px) calc(var(--y, 0) * 1px),
hsl(
var(--hue, 70) calc(var(--saturation, 100) * 100%) calc(var(--lightness, 70) * 1%) /
var(--border-spot-opacity, 1)
),
transparent 100%
);
filter: brightness(2);
}
[data-glow]::after {
background-image: radial-gradient(
calc(var(--spotlight-size) * 0.5) calc(var(--spotlight-size) * 0.5) at calc(var(--x, 0) * 1px)
calc(var(--y, 0) * 1px),
hsl(0 100% 100% / var(--border-light-opacity, 1)),
transparent 100%
);
}
[data-glow] [data-glow] {
position: absolute;
inset: 0;
will-change: filter;
opacity: var(--outer, 1);
}
[data-glow] > [data-glow] {
border-width: calc(var(--border-size) * 20);
filter: blur(calc(var(--border-size) * 10));
background: none;
pointer-events: none;
}
[data-glow] > [data-glow]::before {
inset: -10px;
border-width: 10px;
}
[data-glow] [data-glow] {
border: none;
}
</style>
Loading