Skip to content

Commit

Permalink
Follow the user's github theme instead of system (#444)
Browse files Browse the repository at this point in the history
Fixes #443
  • Loading branch information
pierremtb authored Nov 30, 2023
1 parent e57e7e0 commit b53062b
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 2 deletions.
5 changes: 5 additions & 0 deletions src/chrome/content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
createReactRoot,
getGithubBlobUrlParams,
getGithubCommitWithinPullUrlParams,
getGithubColorMode,
} from './web'
import gitHubInjection from 'github-injection'
import { isFilenameSupported } from './diff'
Expand All @@ -24,12 +25,14 @@ async function injectDiff(
document: Document
) {
const map = mapInjectableDiffElements(document, files)
const colorMode = getGithubColorMode(document)
const cadDiffPage = React.createElement(CadDiffPage, {
owner,
repo,
sha,
parentSha,
map,
colorMode,
})
root.render(cadDiffPage)
}
Expand Down Expand Up @@ -60,13 +63,15 @@ async function injectBlob(
}

element.classList.add('kittycad-injected-file')
const colorMode = getGithubColorMode(document)
const cadBlobPage = React.createElement(CadBlobPage, {
element,
owner,
repo,
sha,
filename,
classicUi,
colorMode,
})
root.render(cadBlobPage)
}
Expand Down
34 changes: 34 additions & 0 deletions src/chrome/web.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
createReactRoot,
getGithubBlobUrlParams,
getGithubCommitWithinPullUrlParams,
getGithubColorMode,
} from './web'

const githubPullHtmlSnippet = `
Expand Down Expand Up @@ -221,3 +222,36 @@ it('adds a div element, creates a react root inside, and can render', () => {
expect(root).toBeDefined()
expect(() => root.render(React.createElement('a'))).not.toThrow()
})

it('finds the right color mode from GitHub', () => {
const lightHtml = `
<html lang="en"
data-color-mode="light" data-light-theme="light" data-dark-theme="dark"
data-a11y-animated-images="system" data-a11y-link-underlines="true">
</html>
`
const lightDocument = parser.parseFromString(lightHtml, 'text/html')
expect(getGithubColorMode(lightDocument)).toEqual('light')

const darkHtml = `
<html lang="en"
data-color-mode="dark" data-light-theme="light" data-dark-theme="dark"
data-a11y-animated-images="system" data-a11y-link-underlines="true">
</html>
`
const darkDocument = parser.parseFromString(darkHtml, 'text/html')
expect(getGithubColorMode(darkDocument)).toEqual('dark')

const sysHtml = `
<html lang="en"
data-color-mode="auto" data-light-theme="light" data-dark-theme="dark"
data-a11y-animated-images="system" data-a11y-link-underlines="true">
</html>
`
const sysDocument = parser.parseFromString(sysHtml, 'text/html')
expect(getGithubColorMode(sysDocument)).toEqual('auto')

const irrelevantHtml = '<html></html>'
const irrDocument = parser.parseFromString(irrelevantHtml, 'text/html')
expect(getGithubColorMode(irrDocument)).toEqual('auto')
})
11 changes: 11 additions & 0 deletions src/chrome/web.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ColorModeWithAuto } from '@primer/react/lib/ThemeProvider'
import { createRoot, Root } from 'react-dom/client'
import { isFilenameSupported, extensionToSrcFormat } from './diff'
import { DiffEntry } from './types'
Expand Down Expand Up @@ -156,3 +157,13 @@ export function createReactRoot(
document.body.appendChild(node)
return createRoot(node)
}

export function getGithubColorMode(document: Document): ColorModeWithAuto {
const html = document.querySelector('html')
const attr = 'data-color-mode'
if (!html || !html.getAttribute(attr)) {
return 'auto'
}

return html.getAttribute(attr) as ColorModeWithAuto
}
1 change: 1 addition & 0 deletions src/components/settings/Settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ export function Settings() {
}, [])

return (
// Setting colorMode to 'auto' as this popup is part of Chrome
<ThemeProvider colorMode="auto">
<Box backgroundColor="canvas.default" width={300} p={4}>
{firstInitDone ? (
Expand Down
5 changes: 4 additions & 1 deletion src/components/viewer/CadBlobPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { FileBlob, MessageIds } from '../../chrome/types'
import { createPortal } from 'react-dom'
import { Loading } from '../Loading'
import { CadBlob } from './CadBlob'
import { ColorModeWithAuto } from '@primer/react/lib/ThemeProvider'

function CadBlobPortal({
element,
Expand Down Expand Up @@ -148,6 +149,7 @@ export type CadBlobPageProps = {
sha: string
filename: string
classicUi: boolean
colorMode: ColorModeWithAuto
}

export function CadBlobPage({
Expand All @@ -157,9 +159,10 @@ export function CadBlobPage({
sha,
filename,
classicUi,
colorMode,
}: CadBlobPageProps): React.ReactElement {
return (
<ThemeProvider colorMode="auto">
<ThemeProvider colorMode={colorMode}>
<CadBlobPortal
element={element}
owner={owner}
Expand Down
5 changes: 4 additions & 1 deletion src/components/viewer/CadDiffPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { createPortal } from 'react-dom'
import { Loading } from '../Loading'
import { CadDiff } from './CadDiff'
import { SourceRichToggle } from './SourceRichToggle'
import { ColorModeWithAuto } from '@primer/react/lib/ThemeProvider'

function CadDiffPortal({
element,
Expand Down Expand Up @@ -107,6 +108,7 @@ export type CadDiffPageProps = {
repo: string
sha: string
parentSha: string
colorMode: ColorModeWithAuto
}

export function CadDiffPage({
Expand All @@ -115,9 +117,10 @@ export function CadDiffPage({
repo,
sha,
parentSha,
colorMode,
}: CadDiffPageProps): React.ReactElement {
return (
<ThemeProvider colorMode="auto">
<ThemeProvider colorMode={colorMode}>
{map.map(m => (
<CadDiffPortal
key={m.file.filename}
Expand Down

0 comments on commit b53062b

Please sign in to comment.