|
| 1 | +<script setup lang="ts"> |
| 2 | +const isMounted = ref(false) |
| 3 | +const isVisible = ref(false) |
| 4 | +const isScrollable = ref(true) |
| 5 | +const lastScrollY = ref(0) |
| 6 | +const footerRef = ref<HTMLElement>() |
| 7 | +
|
| 8 | +// Check if CSS scroll-state container queries are supported |
| 9 | +// Once this becomes baseline, we can remove the JS scroll handling entirely |
| 10 | +const supportsScrollStateQueries = ref(false) |
| 11 | +
|
| 12 | +function checkScrollable() { |
| 13 | + return document.documentElement.scrollHeight > window.innerHeight |
| 14 | +} |
| 15 | +
|
| 16 | +function onScroll() { |
| 17 | + // Skip JS-based visibility logic if CSS scroll-state queries handle it |
| 18 | + if (supportsScrollStateQueries.value) return |
| 19 | +
|
| 20 | + const currentY = window.scrollY |
| 21 | + const diff = lastScrollY.value - currentY |
| 22 | + const nearBottom = currentY + window.innerHeight >= document.documentElement.scrollHeight - 50 |
| 23 | +
|
| 24 | + // Scrolling UP or near bottom -> show |
| 25 | + if (Math.abs(diff) > 10) { |
| 26 | + isVisible.value = diff > 0 || nearBottom |
| 27 | + lastScrollY.value = currentY |
| 28 | + } |
| 29 | +
|
| 30 | + // At top -> hide |
| 31 | + if (currentY < 100) { |
| 32 | + isVisible.value = false |
| 33 | + } |
| 34 | +
|
| 35 | + // Near bottom -> always show |
| 36 | + if (nearBottom) { |
| 37 | + isVisible.value = true |
| 38 | + } |
| 39 | +} |
| 40 | +
|
| 41 | +function updateFooterPadding() { |
| 42 | + const height = isScrollable.value && footerRef.value ? footerRef.value.offsetHeight : 0 |
| 43 | + document.documentElement.style.setProperty('--footer-height', `${height}px`) |
| 44 | +} |
| 45 | +
|
| 46 | +function onResize() { |
| 47 | + isScrollable.value = checkScrollable() |
| 48 | + updateFooterPadding() |
| 49 | +} |
| 50 | +
|
| 51 | +onMounted(() => { |
| 52 | + // Feature detect CSS scroll-state container queries (Chrome 133+) |
| 53 | + // @see https://developer.mozilla.org/en-US/docs/Web/CSS/@container#scroll-state_container_descriptors |
| 54 | + supportsScrollStateQueries.value = CSS.supports('container-type', 'scroll-state') |
| 55 | +
|
| 56 | + nextTick(() => { |
| 57 | + lastScrollY.value = window.scrollY |
| 58 | + isScrollable.value = checkScrollable() |
| 59 | + updateFooterPadding() |
| 60 | + // Only apply dynamic classes after mount to avoid hydration mismatch |
| 61 | + isMounted.value = true |
| 62 | + }) |
| 63 | +
|
| 64 | + window.addEventListener('scroll', onScroll, { passive: true }) |
| 65 | + window.addEventListener('resize', onResize, { passive: true }) |
| 66 | +}) |
| 67 | +
|
| 68 | +onUnmounted(() => { |
| 69 | + window.removeEventListener('scroll', onScroll) |
| 70 | + window.removeEventListener('resize', onResize) |
| 71 | +}) |
| 72 | +</script> |
| 73 | + |
1 | 74 | <template> |
2 | | - <footer class="border-t border-border mt-auto"> |
3 | | - <div class="container py-8 flex flex-col gap-4 text-fg-subtle text-sm"> |
4 | | - <div class="flex flex-col sm:flex-row items-center justify-between gap-4"> |
5 | | - <p class="font-mono m-0">a better browser for the npm registry</p> |
6 | | - <div class="flex items-center gap-6"> |
| 75 | + <footer |
| 76 | + ref="footerRef" |
| 77 | + aria-label="Site footer" |
| 78 | + class="border-t border-border bg-bg/90 backdrop-blur-md" |
| 79 | + :class="[ |
| 80 | + // Only apply dynamic positioning classes after mount to avoid hydration mismatch |
| 81 | + !isMounted |
| 82 | + ? 'mt-auto' |
| 83 | + : // When CSS scroll-state queries are supported, use CSS-only approach |
| 84 | + supportsScrollStateQueries |
| 85 | + ? 'footer-scroll-state' |
| 86 | + : // Fallback to JS-controlled classes |
| 87 | + isScrollable |
| 88 | + ? [ |
| 89 | + 'fixed bottom-0 left-0 right-0 z-40 transition-transform duration-300 ease-out', |
| 90 | + isVisible ? 'translate-y-0' : 'translate-y-full', |
| 91 | + ] |
| 92 | + : 'mt-auto', |
| 93 | + ]" |
| 94 | + > |
| 95 | + <div class="container py-2 sm:py-6 flex flex-col gap-1 sm:gap-3 text-fg-subtle text-sm"> |
| 96 | + <div class="flex flex-row items-center justify-between gap-2 sm:gap-4"> |
| 97 | + <p class="font-mono m-0 hidden sm:block">a better browser for the npm registry</p> |
| 98 | + <!-- On mobile, show disclaimer here instead of tagline --> |
| 99 | + <p class="text-xs text-fg-muted m-0 sm:hidden">not affiliated with npm, Inc.</p> |
| 100 | + <div class="flex items-center gap-4 sm:gap-6"> |
7 | 101 | <a |
8 | 102 | href="https://github.com/npmx-dev/npmx.dev" |
9 | 103 | rel="noopener noreferrer" |
10 | | - class="link-subtle font-mono text-xs" |
| 104 | + class="link-subtle font-mono text-xs min-h-11 min-w-11 flex items-center" |
11 | 105 | > |
12 | 106 | source |
13 | 107 | </a> |
14 | 108 | <span class="text-border">|</span> |
15 | | - <a href="https://roe.dev" rel="noopener noreferrer" class="link-subtle font-mono text-xs"> |
| 109 | + <a |
| 110 | + href="https://roe.dev" |
| 111 | + rel="noopener noreferrer" |
| 112 | + class="link-subtle font-mono text-xs min-h-11 min-w-11 flex items-center" |
| 113 | + > |
16 | 114 | @danielroe |
17 | 115 | </a> |
18 | 116 | </div> |
19 | 117 | </div> |
20 | | - <p class="text-xs text-fg-muted text-center sm:text-left m-0"> |
| 118 | + <p class="text-xs text-fg-muted text-center sm:text-left m-0 hidden sm:block"> |
21 | 119 | npm is a registered trademark of npm, Inc. This site is not affiliated with npm, Inc. |
22 | 120 | </p> |
23 | 121 | </div> |
24 | 122 | </footer> |
25 | 123 | </template> |
| 124 | + |
| 125 | +<style scoped> |
| 126 | +/* |
| 127 | + * CSS scroll-state container queries (Chrome 133+) |
| 128 | + * @see https://developer.mozilla.org/en-US/docs/Web/CSS/@container#scroll-state_container_descriptors |
| 129 | + * |
| 130 | + * This provides a pure CSS solution for showing/hiding the footer based on scroll state. |
| 131 | + * The JS fallback handles browsers without support. |
| 132 | + * Once scroll-state queries become baseline, we can remove the JS scroll handling entirely. |
| 133 | + */ |
| 134 | +@supports (container-type: scroll-state) { |
| 135 | + .footer-scroll-state { |
| 136 | + position: fixed; |
| 137 | + bottom: 0; |
| 138 | + left: 0; |
| 139 | + right: 0; |
| 140 | + z-index: 40; |
| 141 | + /* Hidden by default (translated off-screen) */ |
| 142 | + transform: translateY(100%); |
| 143 | + transition: transform 0.3s ease-out; |
| 144 | + } |
| 145 | +
|
| 146 | + /* Show footer when user can scroll up (meaning they've scrolled down) */ |
| 147 | + @container scroll-state(scrollable: top) { |
| 148 | + .footer-scroll-state { |
| 149 | + transform: translateY(0); |
| 150 | + } |
| 151 | + } |
| 152 | +} |
| 153 | +</style> |
0 commit comments