Skip to content

Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.

Revert "Fade the edges of the top bar when it scrolls on small screens" (#6759)

Adrian Gruntkowskid366b336dc0b
diff --git a/assets/js/dashboard/hooks/use-scroll-fade-mask.ts b/assets/js/dashboard/hooks/use-scroll-fade-mask.ts
deleted file mode 100644
index 4be06ff761..0000000000
--- a/assets/js/dashboard/hooks/use-scroll-fade-mask.ts
+++ /dev/null
@@ -1,60 +0,0 @@
-import { useCallback, useLayoutEffect, useRef, useState } from 'react'
-
-const SCROLL_FADE_PX = 32
-
-type ScrollOverflow = { start: boolean; end: boolean }
-
-const getScrollOverflow = (element: HTMLElement): ScrollOverflow => ({
- start: element.scrollLeft > 1,
- end: element.scrollLeft + element.clientWidth < element.scrollWidth - 1
-})
-
-const getFadeMask = ({ start, end }: ScrollOverflow) => {
- if (!start && !end) {
- return undefined
- }
- const from = start ? `transparent, black ${SCROLL_FADE_PX}px` : 'black'
- const to = end
- ? `black calc(100% - ${SCROLL_FADE_PX}px), transparent`
- : 'black'
- return `linear-gradient(to right, ${from}, ${to})`
-}
-
-export function useScrollFadeMask<T extends HTMLElement>() {
- const ref = useRef<T>(null)
- const [overflow, setOverflow] = useState<ScrollOverflow>({
- start: false,
- end: false
- })
-
- const update = useCallback(() => {
- const element = ref.current
- if (!element) {
- return
- }
- const next = getScrollOverflow(element)
- setOverflow((current) =>
- current.start === next.start && current.end === next.end ? current : next
- )
- }, [])
-
- useLayoutEffect(() => {
- const element = ref.current
- if (!element) {
- return
- }
- // children can grow without resizing the scroll container itself
- const resizeObserver = new ResizeObserver(update)
- resizeObserver.observe(element)
- Array.from(element.children).forEach((child) =>
- resizeObserver.observe(child)
- )
- element.addEventListener('scroll', update, { passive: true })
- return () => {
- resizeObserver.disconnect()
- element.removeEventListener('scroll', update)
- }
- }, [update])
-
- return { ref, maskImage: getFadeMask(overflow), update }
-}
diff --git a/assets/js/dashboard/nav-menu/filters-bar.tsx b/assets/js/dashboard/nav-menu/filters-bar.tsx
index 4e1bf4c278..7f7067c45a 100644
--- a/assets/js/dashboard/nav-menu/filters-bar.tsx
+++ b/assets/js/dashboard/nav-menu/filters-bar.tsx
@@ -1,6 +1,12 @@
import { StarIcon } from '@heroicons/react/24/outline'
import classNames from 'classnames'
-import React, { ReactNode, useEffect, useRef } from 'react'
+import React, {
+ ReactNode,
+ useEffect,
+ useLayoutEffect,
+ useRef,
+ useState
+} from 'react'
import { AppliedFilterPillsList } from './filter-pills-list'
import { FilterMenu } from './filter-menu'
import { useDashboardStateContext } from '../dashboard-state-context'
@@ -15,7 +21,10 @@ import {
import { useRoutelessModalsContext } from '../navigation/routeless-modals-context'
import { DashboardState } from '../dashboard-state'
import { useUserContext } from '../user-context'
-import { useScrollFadeMask } from '../hooks/use-scroll-fade-mask'
+
+const SCROLL_FADE_PX = 32
+
+type ScrollOverflow = { start: boolean; end: boolean }
const canShowClearAllAction = ({
filters
@@ -68,14 +77,56 @@ export const FiltersBar = () => {
)
}
+const getScrollOverflow = (element: HTMLElement): ScrollOverflow => ({
+ start: element.scrollLeft > 1,
+ end: element.scrollLeft + element.clientWidth < element.scrollWidth - 1
+})
+
+const getFadeMask = ({ start, end }: ScrollOverflow) => {
+ if (!start && !end) {
+ return undefined
+ }
+ const from = start ? `transparent, black ${SCROLL_FADE_PX}px` : 'black'
+ const to = end
+ ? `black calc(100% - ${SCROLL_FADE_PX}px), transparent`
+ : 'black'
+ return `linear-gradient(to right, ${from}, ${to})`
+}
+
const ScrollableFilterPills = () => {
const { dashboardState } = useDashboardStateContext()
- const { ref, maskImage, update } = useScrollFadeMask<HTMLDivElement>()
+ const scrollRef = useRef<HTMLDivElement>(null)
+ const [overflow, setOverflow] = useState<ScrollOverflow>({
+ start: false,
+ end: false
+ })
const filtersCount = dashboardState.filters.length
const previousFiltersCount = useRef(filtersCount)
+ const updateOverflow = (element: HTMLElement) => {
+ const next = getScrollOverflow(element)
+ setOverflow((current) =>
+ current.start === next.start && current.end === next.end ? current : next
+ )
+ }
+
+ useLayoutEffect(() => {
+ const element = scrollRef.current
+ if (!element) {
+ return
+ }
+ const onChange = () => updateOverflow(element)
+ const resizeObserver = new ResizeObserver(onChange)
+ resizeObserver.observe(element)
+ element.addEventListener('scroll', onChange, { passive: true })
+ return () => {
+ resizeObserver.disconnect()
+ element.removeEventListener('scroll', onChange)
+ }
+ }, [])
+
useEffect(() => {
- const element = ref.current
+ const element = scrollRef.current
if (!element) {
return
}
@@ -84,14 +135,14 @@ const ScrollableFilterPills = () => {
element.scrollLeft = 0
}
previousFiltersCount.current = filtersCount
- update()
- }, [ref, update, dashboardState.filters, filtersCount])
+ updateOverflow(element)
+ }, [dashboardState.filters, filtersCount])
return (
<AppliedFilterPillsList
- ref={ref}
+ ref={scrollRef}
className="md:overflow-x-auto [scrollbar-width:none] [&::-webkit-scrollbar]:hidden"
- style={{ maskImage }}
+ style={{ maskImage: getFadeMask(overflow) }}
/>
)
}
diff --git a/assets/js/dashboard/nav-menu/top-bar.tsx b/assets/js/dashboard/nav-menu/top-bar.tsx
index 7c2b29269f..1aa47c1819 100644
--- a/assets/js/dashboard/nav-menu/top-bar.tsx
+++ b/assets/js/dashboard/nav-menu/top-bar.tsx
@@ -1,4 +1,4 @@
-import React, { CSSProperties, ReactNode } from 'react'
+import React, { ReactNode } from 'react'
import { SiteSwitcher } from '../site-switcher'
import { useSiteContext } from '../site-context'
import CurrentVisitors from '../stats/current-visitors'
@@ -8,7 +8,6 @@ import { FiltersBar } from './filters-bar'
import { DashboardPeriodPicker } from './query-periods/dashboard-period-picker'
import { SegmentMenu } from './segments/segment-menu'
import { DashboardOptionsMenu } from './dashboard-options-menu'
-import { useScrollFadeMask } from '../hooks/use-scroll-fade-mask'
interface TopBarProps {
showCurrentVisitors: boolean
@@ -44,14 +43,8 @@ function TopBarStickyWrapper({ children }: { children: ReactNode }) {
}
function TopBarInner({ showCurrentVisitors }: TopBarProps) {
- const { ref, maskImage } = useScrollFadeMask<HTMLDivElement>()
-
return (
- <div
- ref={ref}
- className="flex min-w-0 flex-nowrap items-center gap-x-1 overflow-x-auto md:overflow-visible w-full touch-pan-x md:touch-auto [scrollbar-width:none] [-ms-overflow-style:none] [&::-webkit-scrollbar]:hidden py-1 -my-1 md:py-0 md:my-0 max-md:[mask-image:var(--scroll-fade-mask)]"
- style={{ '--scroll-fade-mask': maskImage } as CSSProperties}
- >
+ <div className="flex min-w-0 flex-nowrap items-center gap-x-1 overflow-x-auto md:overflow-visible w-full touch-pan-x md:touch-auto [scrollbar-width:none] [-ms-overflow-style:none] [&::-webkit-scrollbar]:hidden py-1 -my-1 md:py-0 md:my-0">
<div className="flex shrink-0 items-center sm:gap-x-1 md:gap-x-2.5">
<SiteSwitcher />
{showCurrentVisitors && <CurrentVisitors />}