Skip to content

Commit

Permalink
Add light/dark theme toggle to editor octokatherine#29 (octokatherine#38
Browse files Browse the repository at this point in the history
)
  • Loading branch information
saurabhchaturvedy authored Apr 14, 2021
1 parent 5d81ee0 commit 60d8689
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 2 deletions.
24 changes: 22 additions & 2 deletions components/EditorColumn.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export const EditorColumn = ({ focusedSectionSlug, templates, setTemplates }) =>
return section ? section.markdown : ''
}
const [markdown, setMarkdown] = useState(getMarkdown())
const [toggleState, setToggleState] = useState({ theme: 'vs-dark', img: 'toggle_sun.svg' })

useEffect(() => {
const markdown = getMarkdown()
Expand All @@ -25,14 +26,27 @@ export const EditorColumn = ({ focusedSectionSlug, templates, setTemplates }) =>
})
}

const toggleTheme = () => {
toggleDarkMode(toggleState, setToggleState)
}

return (
<div className="flex-1 px-3 full-screen max-w-[50%] min-w-[500px]">
<h3 className="mb-3 text-lg font-medium leading-6 text-gray-900">Editor</h3>
<h3 className="mb-3 text-lg font-medium leading-6 text-gray-900">
Editor
<button
onClick={toggleTheme}
aria-label="Color Mode"
className="toggle-dark-mode focus:outline-none transition transform hover:-translate-y-1 motion-reduce:transition-none motion-reduce:transform-none"
>
<img className="w-auto h-8 mr-2" src={toggleState.img} />
</button>
</h3>
{focusedSectionSlug ? (
<Editor
wrapperClassName="rounded-sm border border-gray-500"
className="full-screen" // By default, it fully fits with its parent
theme="vs-dark"
theme={toggleState.theme}
language="markdown"
value={markdown}
onChange={onEdit}
Expand All @@ -52,3 +66,9 @@ export const EditorColumn = ({ focusedSectionSlug, templates, setTemplates }) =>
</div>
)
}

const toggleDarkMode = (toggleState, setToggleState) => {
toggleState.theme == 'vs-dark'
? setToggleState({ theme: 'light', img: 'toggle_moon.svg' })
: setToggleState({ theme: 'vs-dark', img: 'toggle_sun.svg' })
}
1 change: 1 addition & 0 deletions public/toggle_moon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions public/toggle_sun.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions styles/globals.css
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
max-height: calc(100vh - 160px);
}

.toggle-dark-mode {
float: right;
}

@layer base {
html {
@apply bg-gray-50;
Expand Down

0 comments on commit 60d8689

Please sign in to comment.