Skip to content
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

Default day/night theme to system theme via css3 media query. #215

Merged
merged 1 commit into from
Jun 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
80 changes: 40 additions & 40 deletions docs/index.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/index.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bldrs",
"version": "1.0.0-r298",
"version": "1.0.0-r302",
"main": "src/index.jsx",
"homepage": "https://github.com/bldrs-ai/Share",
"bugs": {
Expand Down
19 changes: 17 additions & 2 deletions src/Theme.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ export default function useTheme() {
const [mode, setMode] = useState(Privacy.getCookie({
component: 'theme',
name: 'mode',
defaultValue: Themes.Day}))
defaultValue: getSystemCurrentLightDark(),
}))


const theme = useMemo(() => {
Expand Down Expand Up @@ -48,7 +49,7 @@ export default function useTheme() {
}


const Themes = {
export const Themes = {
Day: 'Day',
Night: 'Night',
}
Expand Down Expand Up @@ -103,3 +104,17 @@ function loadTheme(mode) {
}
return createTheme(theme)
}


/**
* Look for explicit night, otherwise day
* See https://drafts.csswg.org/mediaqueries-5/#prefers-color-scheme
* @return {string}
* @private
*/
export function getSystemCurrentLightDark() {
if (window.matchMedia) {
return window.matchMedia('(prefers-color-scheme: dark)').matches ? Themes.Night : Themes.Day
}
return Themes.Day
}
35 changes: 35 additions & 0 deletions src/Theme.test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import {Themes, getSystemCurrentLightDark} from './Theme'


// https://stackoverflow.com/questions/41885841/how-can-i-mock-the-javascript-window-object-using-jest
let windowSpy

beforeEach(() => {
windowSpy = jest.spyOn(window, 'window', 'get')
})


afterEach(() => {
windowSpy.mockRestore()
})


describe('Theme', () => {
test('getSystemCurrentLightDark is day when system is day', () => {
windowSpy.mockImplementation(() => ({
matchMedia: jest.fn((query) => {
return {matches: query == '(prefers-color-scheme: dark)' ? false : true}
}),
}))
expect(getSystemCurrentLightDark()).toBe(Themes.Day)
})

test('getSystemCurrentLightDark is night when system is night', () => {
windowSpy.mockImplementation(() => ({
matchMedia: jest.fn((query) => {
return {matches: query == '(prefers-color-scheme: dark)' ? true : false}
}),
}))
expect(getSystemCurrentLightDark()).toBe(Themes.Night)
})
})