-
-
Notifications
You must be signed in to change notification settings - Fork 32.7k
[docs-infra] Reapply Cookie Banner with Design Fixes #47744
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
Open
dav-is
wants to merge
12
commits into
mui:master
Choose a base branch
from
dav-is:davis/reapply-cookie-banner
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
221af3b
Reapply cookie banner
dav-is 78290b3
Add consent dialog to AppFrame
dav-is 9adf72f
Merge branch 'master' into davis/reapply-cookie-banner
dav-is 6632a12
Merge branch 'master' into davis/reapply-cookie-banner
dav-is 5c99772
Use BrandingCssThemeProvider
dav-is 41545cd
Merge branch 'davis/reapply-cookie-banner' of github.com:dav-is/mater…
dav-is cdf64f1
Merge branch 'master' into davis/reapply-cookie-banner
dav-is f2c973a
Merge branch 'master' into davis/reapply-cookie-banner
dav-is 92e068d
Merge branch 'master' into davis/reapply-cookie-banner
dav-is 29c430a
Apply suggestions from code review
dav-is 199abaa
Merge branch 'master' into davis/reapply-cookie-banner
dav-is 5e68ed3
Prettier
dav-is File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,219 @@ | ||
| import * as React from 'react'; | ||
| import Button from '@mui/material/Button'; | ||
| import Fade from '@mui/material/Fade'; | ||
| import Paper from '@mui/material/Paper'; | ||
| import Box from '@mui/material/Box'; | ||
| import Stack from '@mui/material/Stack'; | ||
| import Typography from '@mui/material/Typography'; | ||
| import useLocalStorageState from '@mui/utils/useLocalStorageState'; | ||
| import { alpha } from '@mui/system'; | ||
| import Portal from '@mui/material/Portal'; | ||
| import TrapFocus from '@mui/material/Unstable_TrapFocus'; | ||
| import CookieOutlinedIcon from '@mui/icons-material/CookieOutlined'; | ||
| import { BrandingCssThemeProvider } from 'docs/src/BrandingCssVarsProvider'; | ||
|
|
||
| const COOKIE_CONSENT_KEY = 'docs-cookie-consent'; | ||
|
|
||
| type ConsentStatus = 'analytics' | 'essential' | null; | ||
|
|
||
| function getDoNotTrack(): boolean { | ||
| if (typeof window === 'undefined') { | ||
| return false; | ||
| } | ||
| // Check for Do Not Track (DNT) | ||
| return navigator.doNotTrack === '1' || (window as { doNotTrack?: string }).doNotTrack === '1'; | ||
| } | ||
|
|
||
| // DNT doesn't change during a session, so we can use a simple external store | ||
| const subscribeToNothing = () => () => {}; | ||
| const getDoNotTrackSnapshot = () => getDoNotTrack(); | ||
| const getDoNotTrackServerSnapshot = () => true; // Assume DNT until we know the actual value | ||
|
|
||
| function useDoNotTrack(): boolean { | ||
| return React.useSyncExternalStore( | ||
| subscribeToNothing, | ||
| getDoNotTrackSnapshot, | ||
| getDoNotTrackServerSnapshot, | ||
| ); | ||
| } | ||
|
|
||
| interface AnalyticsContextValue { | ||
| consentStatus: ConsentStatus; | ||
| hasAnalyticsConsent: boolean; | ||
| needsConsent: boolean; | ||
| setAnalyticsConsent: () => void; | ||
| setEssentialOnly: () => void; | ||
| } | ||
|
|
||
| const AnalyticsContext = React.createContext<AnalyticsContextValue>({ | ||
| consentStatus: null, | ||
| hasAnalyticsConsent: false, | ||
| needsConsent: false, | ||
| setAnalyticsConsent: () => {}, | ||
| setEssentialOnly: () => {}, | ||
| }); | ||
|
|
||
| export function useAnalyticsConsent() { | ||
| return React.useContext(AnalyticsContext); | ||
| } | ||
|
|
||
| export function CookieConsentDialog() { | ||
| const { needsConsent, setAnalyticsConsent, setEssentialOnly } = useAnalyticsConsent(); | ||
| const [show, setShow] = React.useState(false); | ||
|
|
||
| React.useEffect(() => { | ||
| if (needsConsent) { | ||
| // Double rAF to ensure the initial opacity: 0 state is painted before transitioning | ||
| const frame = requestAnimationFrame(() => { | ||
| requestAnimationFrame(() => { | ||
| setShow(true); | ||
| }); | ||
| }); | ||
| return () => cancelAnimationFrame(frame); | ||
| } | ||
| setShow(false); | ||
| return undefined; | ||
| }, [needsConsent]); | ||
|
|
||
| return ( | ||
| <Portal> | ||
| <TrapFocus open={needsConsent} disableAutoFocus disableEnforceFocus> | ||
| <Fade in={show} unmountOnExit> | ||
| <Paper | ||
| role="dialog" | ||
| aria-modal="false" | ||
| aria-labelledby="cookie-consent-dialog-title" | ||
| aria-describedby="cookie-consent-dialog-description" | ||
| variant="outlined" | ||
| tabIndex={-1} | ||
| sx={(theme) => ({ | ||
| position: 'fixed', | ||
| bottom: 0, | ||
| right: 0, | ||
| p: 2, | ||
| m: 2, | ||
| maxWidth: 340, | ||
| pointerEvents: 'auto', | ||
| boxShadow: theme.shadows[2], | ||
| zIndex: theme.zIndex.snackbar, | ||
| ...theme.applyDarkStyles({ | ||
| bgcolor: 'primaryDark.900', | ||
| }), | ||
| })} | ||
| > | ||
| <Stack direction="column" spacing={3} sx={{ justifyContent: 'flex-start' }}> | ||
| <Stack | ||
| spacing={1} | ||
| sx={{ flexShrink: 1, alignSelf: { xs: 'flex-start', sm: 'center' } }} | ||
| > | ||
| <Box | ||
| sx={(theme) => ({ | ||
| borderRadius: '50%', | ||
| bgcolor: alpha(theme.palette.primary.main, 0.1), | ||
| p: 1, | ||
| display: 'inline-block', | ||
| width: 40, | ||
| height: 40, | ||
| mb: -1, | ||
| alignSelf: { xs: 'center', sm: 'flex-start' }, | ||
| })} | ||
| > | ||
| <CookieOutlinedIcon color="primary" strokeWidth={1.5} /> | ||
| </Box> | ||
| <Stack spacing={0.5}> | ||
| <Typography | ||
| variant="subtitle2" | ||
| id="cookie-consent-dialog-title" | ||
| textAlign={{ xs: 'center', sm: 'start' }} | ||
| > | ||
| Cookie Preferences | ||
| </Typography> | ||
| <Typography | ||
| id="cookie-consent-dialog-description" | ||
| variant="body2" | ||
| textAlign={{ xs: 'center', sm: 'start' }} | ||
| > | ||
| We use cookies to understand site usage and improve our content. This includes | ||
| third-party analytics. | ||
| </Typography> | ||
| </Stack> | ||
| </Stack> | ||
|
|
||
| <Stack direction="row" spacing={1} sx={{ justifyContent: 'flex-start' }}> | ||
| <Button onClick={setAnalyticsConsent} variant="contained" size="small"> | ||
| Allow analytics | ||
| </Button> | ||
| <Button onClick={setEssentialOnly} size="small"> | ||
| Essential only | ||
| </Button> | ||
| </Stack> | ||
| </Stack> | ||
| </Paper> | ||
| </Fade> | ||
| </TrapFocus> | ||
| </Portal> | ||
| ); | ||
| } | ||
|
|
||
| function updateGoogleConsent(hasAnalytics: boolean) { | ||
| if (typeof window !== 'undefined' && typeof window.gtag === 'function') { | ||
| window.gtag('consent', 'update', { | ||
| ad_storage: 'denied', | ||
| ad_user_data: 'denied', | ||
| ad_personalization: 'denied', | ||
| analytics_storage: hasAnalytics ? 'granted' : 'denied', | ||
| }); | ||
|
|
||
| // Initialize Apollo when analytics consent is granted | ||
| const win = window as typeof window & { initApollo?: () => void }; | ||
| if (hasAnalytics && typeof win.initApollo === 'function') { | ||
| win.initApollo(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| export function AnalyticsProvider({ children }: { children: React.ReactNode }) { | ||
| const [consentStatus, setConsentStatus] = useLocalStorageState(COOKIE_CONSENT_KEY, null); | ||
| const doNotTrack = useDoNotTrack(); | ||
|
|
||
| // Respect Do Not Track - don't show dialog and treat as essential only | ||
| const needsConsent = consentStatus === null && !doNotTrack; | ||
|
|
||
| // Update Google consent when status changes or on mount if already set | ||
| React.useEffect(() => { | ||
| if (doNotTrack) { | ||
| // DNT is enabled - always deny analytics | ||
| updateGoogleConsent(false); | ||
| } else if (consentStatus !== null) { | ||
| updateGoogleConsent(consentStatus === 'analytics'); | ||
| } | ||
| }, [consentStatus, doNotTrack]); | ||
|
|
||
| const setAnalyticsConsent = React.useCallback(() => { | ||
| setConsentStatus('analytics'); | ||
| }, [setConsentStatus]); | ||
|
|
||
| const setEssentialOnly = React.useCallback(() => { | ||
| setConsentStatus('essential'); | ||
| }, [setConsentStatus]); | ||
|
|
||
| const contextValue = React.useMemo<AnalyticsContextValue>( | ||
| () => ({ | ||
| consentStatus: doNotTrack ? 'essential' : (consentStatus as ConsentStatus), | ||
| hasAnalyticsConsent: !doNotTrack && consentStatus === 'analytics', | ||
| needsConsent, | ||
| setAnalyticsConsent, | ||
| setEssentialOnly, | ||
| }), | ||
| [consentStatus, doNotTrack, needsConsent, setAnalyticsConsent, setEssentialOnly], | ||
| ); | ||
|
|
||
| return ( | ||
| <AnalyticsContext.Provider value={contextValue}> | ||
| {children} | ||
| <BrandingCssThemeProvider> | ||
| <CookieConsentDialog /> | ||
| </BrandingCssThemeProvider> | ||
| </AnalyticsContext.Provider> | ||
| ); | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.