/* Base scroll-shadow component */ .scroll-shadow { @apply relative; --scroll-shadow-size: 40px; --scroll-shadow-scrollbar-size: 10px; --scroll-shadow-offset: 0px; } /* Fade sizes for the scroll-driven variant below. They stay at 0px whenever the scroll timeline is inactive, which is exactly the state of a container without overflow. */ @property --scroll-shadow-start-fade { syntax: ""; inherits: false; initial-value: 0px; } @property --scroll-shadow-end-fade { syntax: ""; inherits: false; initial-value: 0px; } @keyframes scroll-shadow-start-fade { from { --scroll-shadow-start-fade: 0px; } to { --scroll-shadow-start-fade: var(--scroll-shadow-size); } } @keyframes scroll-shadow-end-fade { from { --scroll-shadow-end-fade: var(--scroll-shadow-size); } to { --scroll-shadow-end-fade: 0px; } } /* Orientation: Vertical */ .scroll-shadow--vertical { @apply scrollbar overflow-y-auto; } /* Orientation: Horizontal */ .scroll-shadow--horizontal { @apply scrollbar overflow-x-auto; } /* Variant: Fade - Vertical */ .scroll-shadow--fade.scroll-shadow--vertical { /* `[data-scroll-shadow-mode="auto"]` lets the scroll-driven variant below reuse this plumbing. It only paints once `--scroll-linear-gradient` is set, since an undefined variable makes `mask-image` invalid at computed-value time, which resolves to `none`. The gradient layer covers the whole box and the opaque scrollbar-lane layer sits on top of it. Splitting the box between the two layers instead leaves the device pixel on the boundary partly covered by each at fractional pixel ratios, which paints a faint line. */ &:where( [data-top-scroll="true"], [data-bottom-scroll="true"], [data-top-bottom-scroll="true"], [data-scroll-shadow-mode="auto"] ) { mask-image: linear-gradient(var(--scroll-linear-gradient)), linear-gradient(#000, #000); mask-position: left top, right top; mask-repeat: no-repeat; mask-size: 100% 100%, var(--scroll-shadow-scrollbar-size) 100%; -webkit-mask-image: linear-gradient(var(--scroll-linear-gradient)), linear-gradient(#000, #000); -webkit-mask-position: left top, right top; -webkit-mask-repeat: no-repeat; -webkit-mask-size: 100% 100%, var(--scroll-shadow-scrollbar-size) 100%; } &[data-top-scroll="true"] { --scroll-linear-gradient: 0deg, #000 calc(100% - var(--scroll-shadow-size)), transparent; } &[data-bottom-scroll="true"] { --scroll-linear-gradient: 180deg, #000 calc(100% - var(--scroll-shadow-size)), transparent; } &[data-top-bottom-scroll="true"] { --scroll-linear-gradient: #000, #000, transparent 0, #000 var(--scroll-shadow-size), #000 calc(100% - var(--scroll-shadow-size)), transparent; } } /* Variant: Fade - Horizontal */ .scroll-shadow--fade.scroll-shadow--horizontal { &:where( [data-left-scroll="true"], [data-right-scroll="true"], [data-left-right-scroll="true"], [data-scroll-shadow-mode="auto"] ) { mask-image: linear-gradient(var(--scroll-linear-gradient)), linear-gradient(#000, #000); mask-position: left top, left bottom; mask-repeat: no-repeat; mask-size: 100% 100%, 100% var(--scroll-shadow-scrollbar-size); -webkit-mask-image: linear-gradient(var(--scroll-linear-gradient)), linear-gradient(#000, #000); -webkit-mask-position: left top, left bottom; -webkit-mask-repeat: no-repeat; -webkit-mask-size: 100% 100%, 100% var(--scroll-shadow-scrollbar-size); } /* `left`/`right` data attributes are logical (before/end of scroll). In RTL the start edge is on the right, so the physical fade direction must be swapped. */ &[data-left-scroll="true"] { --scroll-linear-gradient: 270deg, #000 calc(100% - var(--scroll-shadow-size)), transparent; &:dir(rtl) { --scroll-linear-gradient: 90deg, #000 calc(100% - var(--scroll-shadow-size)), transparent; } } &[data-right-scroll="true"] { --scroll-linear-gradient: 90deg, #000 calc(100% - var(--scroll-shadow-size)), transparent; &:dir(rtl) { --scroll-linear-gradient: 270deg, #000 calc(100% - var(--scroll-shadow-size)), transparent; } } &[data-left-right-scroll="true"] { --scroll-linear-gradient: to right, #000, #000, transparent 0, #000 var(--scroll-shadow-size), #000 calc(100% - var(--scroll-shadow-size)), transparent; } } .scroll-shadow--hide-scrollbar { @apply scrollbar-none; --scroll-shadow-scrollbar-size: 0px; } /* Variant: Fade - scroll-driven Where scroll timelines are available the fade is derived from the real scroll state, so it is already correct on the first paint and needs no measurement or hydration. A container without overflow has an inactive timeline, which leaves both fades at 0px and collapses the mask to a no-op (see the no-overflow rule at the end of this block for timelines that become inactive later). The attribute-driven rules above remain the fallback for engines without scroll timelines, and keep serving controlled mode. Two consequences of deriving the mask from CSS alone: - Auto mode always resolves a `mask-image`, even with nothing to scroll, so the root is always a stacking context and a containing block for fixed descendants. Gating that on overflow would need a measurement, which is the flicker this variant exists to avoid. - The `animation` shorthand here lives in the components layer, so an `animate-*` utility on the root replaces it and pins both fades at 0px, leaving no fade at all. */ @supports (animation-timeline: scroll(self)) { /* `.scroll-shadow` is repeated so these rules outrank the attribute-driven gradients above on specificity rather than on source order, which a re-ordering could break. */ .scroll-shadow.scroll-shadow--fade[data-scroll-shadow-mode="auto"] { animation: scroll-shadow-start-fade linear both, scroll-shadow-end-fade linear both; animation-range: var(--scroll-shadow-offset) calc(var(--scroll-shadow-offset) + var(--scroll-shadow-size)), calc(100% - var(--scroll-shadow-size) - var(--scroll-shadow-offset)) calc(100% - var(--scroll-shadow-offset)); } /* The orientation rules also outrank the `animation` shorthand above, which resets `animation-timeline` to `auto`, so the timeline survives regardless of order. */ .scroll-shadow.scroll-shadow--fade.scroll-shadow--vertical[data-scroll-shadow-mode="auto"] { animation-timeline: scroll(self block), scroll(self block); --scroll-linear-gradient: 180deg, transparent 0, #000 var(--scroll-shadow-start-fade), #000 calc(100% - var(--scroll-shadow-end-fade)), transparent 100%; } .scroll-shadow.scroll-shadow--fade.scroll-shadow--horizontal[data-scroll-shadow-mode="auto"] { animation-timeline: scroll(self inline), scroll(self inline); --scroll-linear-gradient: 90deg, transparent 0, #000 var(--scroll-shadow-start-fade), #000 calc(100% - var(--scroll-shadow-end-fade)), transparent 100%; /* Inline-axis progress starts at the right edge in RTL, so flip the physical direction */ &:dir(rtl) { --scroll-linear-gradient: 270deg, transparent 0, #000 var(--scroll-shadow-start-fade), #000 calc(100% - var(--scroll-shadow-end-fade)), transparent 100%; } } /* A timeline that goes inactive after it was active (content that overflowed, was scrolled, then shrank to fit) keeps its last fade values in Chromium and WebKit instead of falling back to 0px. Once the hook has measured no overflow, drop the animation so both fades return to their 0px initial value. First paint carries no state attributes, so it stays measurement-free. */ .scroll-shadow.scroll-shadow--fade.scroll-shadow--vertical[data-scroll-shadow-mode="auto"][data-top-scroll="false"][data-bottom-scroll="false"], .scroll-shadow.scroll-shadow--fade.scroll-shadow--horizontal[data-scroll-shadow-mode="auto"][data-left-scroll="false"][data-right-scroll="false"] { animation: none; } }