|
| 1 | +--- |
| 2 | +import NewsletterForm from './NewsletterForm.astro' |
| 3 | +--- |
| 4 | + |
| 5 | +<aside class="newsletter-cta" data-newsletter-cta aria-label="Newsletter signup"> |
| 6 | + <div data-newsletter-cta-content> |
| 7 | + <p class="newsletter-cta__headline">Enjoying this? Get Cmdr updates in your inbox.</p> |
| 8 | + <p class="newsletter-cta__subline">Product news, tips, and behind-the-scenes stories. No spam, ever.</p> |
| 9 | + |
| 10 | + <div class="newsletter-cta__form"> |
| 11 | + <NewsletterForm variant="inline" /> |
| 12 | + </div> |
| 13 | + |
| 14 | + <div class="newsletter-cta__dismiss-row"> |
| 15 | + <button type="button" class="newsletter-cta__not-interested" data-newsletter-cta-dismiss> |
| 16 | + Not interested |
| 17 | + </button> |
| 18 | + </div> |
| 19 | + </div> |
| 20 | + |
| 21 | + <p class="newsletter-cta__confirmation" data-newsletter-cta-confirmation> |
| 22 | + Hidden across the site. Changed your mind? There's a signup in the footer. |
| 23 | + </p> |
| 24 | +</aside> |
| 25 | + |
| 26 | +<style> |
| 27 | + .newsletter-cta { |
| 28 | + position: relative; |
| 29 | + margin: 2rem 0; |
| 30 | + padding: 1.5rem; |
| 31 | + border-radius: 0.75rem; |
| 32 | + border: 1px solid var(--color-border); |
| 33 | + border-top: 2px solid var(--color-accent); |
| 34 | + background: var(--color-surface); |
| 35 | + } |
| 36 | + |
| 37 | + .newsletter-cta[data-dismissed] { |
| 38 | + display: none; |
| 39 | + } |
| 40 | + |
| 41 | + .newsletter-cta__headline { |
| 42 | + margin: 0 0 0.25rem; |
| 43 | + font-size: 1rem; |
| 44 | + font-weight: 600; |
| 45 | + color: var(--color-text-primary); |
| 46 | + } |
| 47 | + |
| 48 | + .newsletter-cta__subline { |
| 49 | + margin: 0 0 1rem; |
| 50 | + font-size: 0.875rem; |
| 51 | + color: var(--color-text-secondary); |
| 52 | + } |
| 53 | + |
| 54 | + .newsletter-cta__form { |
| 55 | + max-width: 28rem; |
| 56 | + } |
| 57 | + |
| 58 | + .newsletter-cta__dismiss-row { |
| 59 | + margin-top: 0.75rem; |
| 60 | + text-align: right; |
| 61 | + } |
| 62 | + |
| 63 | + .newsletter-cta__not-interested { |
| 64 | + background: none; |
| 65 | + border: none; |
| 66 | + color: var(--color-text-tertiary); |
| 67 | + font-size: 0.8125rem; |
| 68 | + cursor: pointer; |
| 69 | + padding: 0.25rem 0.5rem; |
| 70 | + text-decoration: none; |
| 71 | + transition: text-decoration var(--duration-normal); |
| 72 | + } |
| 73 | + |
| 74 | + .newsletter-cta__not-interested:hover { |
| 75 | + text-decoration: underline; |
| 76 | + text-underline-offset: 2px; |
| 77 | + } |
| 78 | + |
| 79 | + .newsletter-cta__not-interested:focus-visible { |
| 80 | + outline: 2px solid var(--color-accent); |
| 81 | + outline-offset: 2px; |
| 82 | + } |
| 83 | + |
| 84 | + .newsletter-cta__confirmation { |
| 85 | + display: none; |
| 86 | + font-size: 0.8125rem; |
| 87 | + color: var(--color-text-secondary); |
| 88 | + margin: 0; |
| 89 | + transition: opacity var(--duration-slow); |
| 90 | + } |
| 91 | + |
| 92 | + .newsletter-cta__confirmation[data-visible] { |
| 93 | + display: block; |
| 94 | + } |
| 95 | + |
| 96 | + .newsletter-cta__confirmation[data-fading] { |
| 97 | + opacity: 0; |
| 98 | + } |
| 99 | + |
| 100 | + [data-newsletter-cta-content][data-hidden] { |
| 101 | + display: none; |
| 102 | + } |
| 103 | + |
| 104 | + @media (prefers-reduced-motion: reduce) { |
| 105 | + .newsletter-cta, |
| 106 | + .newsletter-cta__confirmation { |
| 107 | + transition: none !important; |
| 108 | + } |
| 109 | + } |
| 110 | +</style> |
| 111 | + |
| 112 | +<script> |
| 113 | + function isNewsletterHidden(): boolean { |
| 114 | + return ( |
| 115 | + localStorage.getItem('newsletter-subscribed') === 'true' || |
| 116 | + localStorage.getItem('newsletter-dismissed') === 'true' || |
| 117 | + localStorage.getItem('newsletter-cta-dismissed') === 'true' |
| 118 | + ) |
| 119 | + } |
| 120 | + |
| 121 | + function hideConfirmationAfterDelay(confirmation: HTMLElement) { |
| 122 | + const prefersReducedMotion = window.matchMedia('(prefers-reduced-motion: reduce)').matches |
| 123 | + if (prefersReducedMotion) { |
| 124 | + confirmation.removeAttribute('data-visible') |
| 125 | + return |
| 126 | + } |
| 127 | + |
| 128 | + setTimeout(() => { |
| 129 | + confirmation.setAttribute('data-fading', '') |
| 130 | + setTimeout(() => { |
| 131 | + confirmation.removeAttribute('data-visible') |
| 132 | + confirmation.removeAttribute('data-fading') |
| 133 | + }, 500) |
| 134 | + }, 3000) |
| 135 | + } |
| 136 | + |
| 137 | + document.addEventListener('astro:page-load', () => { |
| 138 | + const ctas = document.querySelectorAll<HTMLElement>('[data-newsletter-cta]') |
| 139 | + |
| 140 | + if (isNewsletterHidden()) { |
| 141 | + ctas.forEach((cta) => { |
| 142 | + cta.dataset.dismissed = '' |
| 143 | + }) |
| 144 | + return |
| 145 | + } |
| 146 | + |
| 147 | + ctas.forEach((cta) => { |
| 148 | + const dismissButton = cta.querySelector<HTMLButtonElement>('[data-newsletter-cta-dismiss]') |
| 149 | + if (!dismissButton || dismissButton.dataset.bound) return |
| 150 | + dismissButton.dataset.bound = 'true' |
| 151 | + |
| 152 | + dismissButton.addEventListener('click', () => { |
| 153 | + localStorage.setItem('newsletter-dismissed', 'true') |
| 154 | + |
| 155 | + // Hide content, show confirmation in this CTA |
| 156 | + const content = cta.querySelector<HTMLElement>('[data-newsletter-cta-content]') |
| 157 | + const confirmation = cta.querySelector<HTMLElement>('[data-newsletter-cta-confirmation]') |
| 158 | + if (content) content.setAttribute('data-hidden', '') |
| 159 | + if (confirmation) { |
| 160 | + confirmation.setAttribute('data-visible', '') |
| 161 | + hideConfirmationAfterDelay(confirmation) |
| 162 | + } |
| 163 | + |
| 164 | + // Hide all other CTAs on the page immediately |
| 165 | + const allCtas = document.querySelectorAll<HTMLElement>('[data-newsletter-cta]') |
| 166 | + allCtas.forEach((el) => { |
| 167 | + if (el !== cta) { |
| 168 | + el.dataset.dismissed = '' |
| 169 | + } |
| 170 | + }) |
| 171 | + |
| 172 | + // Also hide any inline newsletter wrappers and header nav items |
| 173 | + document.querySelectorAll<HTMLElement>('[data-newsletter-inline]').forEach((el) => { |
| 174 | + const otherContent = el.querySelector<HTMLElement>('[data-newsletter-inline-content]') |
| 175 | + if (otherContent) otherContent.setAttribute('data-hidden', '') |
| 176 | + }) |
| 177 | + document.querySelectorAll<HTMLElement>('[data-newsletter-nav]').forEach((el) => { |
| 178 | + el.style.display = 'none' |
| 179 | + }) |
| 180 | + }) |
| 181 | + }) |
| 182 | + }) |
| 183 | +</script> |
0 commit comments