-
Notifications
You must be signed in to change notification settings - Fork 408
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
fix: change favicon per theme #1348
Conversation
Branch preview✅ Deploy successful! https://fixchangefaviconpertheme--webcore.review-web-core.5afe.dev |
ESLint Summary View Full Report
Report generated by eslint-plus-action |
import { renderHook } from '@/tests/test-utils' | ||
|
||
describe('usePrefersColorScheme', () => { | ||
it('should return the initial color scheme', () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we add more tests for the listeners of the media query which would set the result to dark / light?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added. I found it a bit difficult to mock the media query event without adding the dependency. Maybe we can look at it quickly together
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can have a look later to do it as described here:
https://jestjs.io/docs/26.x/manual-mocks#mocking-methods-which-are-not-implemented-in-jsdom
<link rel="shortcut icon" href="/favicons/favicon.ico" /> | ||
<link rel="apple-touch-icon" sizes="180x180" href="/favicons/apple-touch-icon.png" /> | ||
<link rel="icon" type="image/png" sizes="32x32" href="/favicons/favicon-32x32.png" /> | ||
<link rel="icon" type="image/png" sizes="16x16" href="/favicons/favicon-16x16.png" /> | ||
<link rel="mask-icon" href="/favicons/safari-pinned-tab.svg" color="#000" /> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we not need to update these other favicons?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've added dark versions to the ones that were subject to poor contrast in browser's tabs.
If I get it correctly the ones I left in <MetaTags>
are for other uses.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm still seeing the old icon in Firefox
Must be the cache
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I opened it in a Private Window though.
src/pages/_app.tsx
Outdated
<link rel="icon" type="image/png" sizes="32x32" href={`/favicons/favicon${darkPath}-32x32.png`} /> | ||
<link rel="icon" type="image/png" sizes="16x16" href={`/favicons/favicon${darkPath}-16x16.png`} /> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we not keep these and associated logic inside MetaTag
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried to include the logic inside <MetaTag />
component but it was not taking effect. I tried to log from inside that component but unsuccessfully. Happy to look at it together if we want to move these <link>
back there
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would be best to keep them together. I am happy to look at it with you.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You propose adding 128 lines of JS and an external library to change a favicon.
From the maintainability standpoint, I would much prefer a low-tech solution like an icon with a background.
src/hooks/usePrefersColorScheme.ts
Outdated
const isDark = window.matchMedia('(prefers-color-scheme: dark)') | ||
const isLight = window.matchMedia('(prefers-color-scheme: light)') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Aren't they mutually exclusive?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From my understanding of https://w3c.github.io/csswg-drafts/mediaqueries-5/#prefers-color-scheme they are not mutually exclusive but correct me if I'm wrong. The user can prefer dark
or light
or have no preference.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, but you don't have a third icon for !isDark && !isLight
, do you?
Also from the docs you linked (emphasis mine):
light
Indicates that user has expressed the preference for a page that has a light theme (dark text on light background), or has not expressed an active preference (and thus should receive the "web default" of a light theme).
src/pages/_app.tsx
Outdated
@@ -77,11 +78,16 @@ interface WebCoreAppProps extends AppProps { | |||
} | |||
|
|||
const WebCoreApp = ({ Component, pageProps, emotionCache = clientSideEmotionCache }: WebCoreAppProps): ReactElement => { | |||
const prefersColorScheme = usePrefersColorScheme() | |||
const darkPath = prefersColorScheme === 'dark' ? '-dark' : '' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
darkPath
is not the best name if it can be either dark or light.
There's a CSS-only solution too, have you tried it? |
I know. Well, the changes are more numerous given the added hook and the test coverage. The dev dependency is also only used for tests and it was added following the code review. I will talk with @liliiaorlenko to get a |
I was trying this implementation but I don't think it suits our use case give it builds over the assumption that nowadays you can use an SVG as a favicon for your website, which is hardly true. I also got the favicon with the green background from the designer. I will create a different PR for it so we can compare the 2 solutions |
I've addressed the comments and opened an alternative implementation to this PR using the favicon with the green background #1376 |
It wasn't necessary to spend time addressing the comments. Closing in favor of the other PR. |
What it solves
Resolves #1123
How this PR fixes it
Implements a hook to detect the system's preferred color scheme and subscribes to that media query changes
How to test it
Go to your OS preferences and change the dark/light theme.
Observe the favicon in the browsers tab changing accordingly.
Screenshots