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

Add iOS style dark/light theme toggle #118

Merged
merged 10 commits into from
Jan 19, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Tweaks
  • Loading branch information
gaearon committed Jan 19, 2019
commit 0340655ba059dca780172eaa4c6e1bb92d253f9a
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
"react": "^16.8.0-alpha.0",
"react-dom": "^16.8.0-alpha.0",
"react-helmet": "^5.2.0",
"react-toggle": "^4.0.2",
"react-typography": "^0.16.13",
"typeface-merriweather": "0.0.43",
"typeface-montserrat": "0.0.43",
Expand Down
2 changes: 1 addition & 1 deletion src/components/Bio.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Bio extends React.Component {
<div
style={{
display: 'flex',
marginBottom: rhythm(2.5),
marginBottom: rhythm(2),
}}
>
<img
Expand Down
28 changes: 22 additions & 6 deletions src/components/Layout.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import React from 'react';
import { Link } from 'gatsby';
import Toggle from 'react-toggle';
import 'react-toggle/style.css';
import Toggle from './Toggle';

import { rhythm, scale } from '../utils/typography';
import { ThemeContext } from './ContextWrapper';
Expand Down Expand Up @@ -42,13 +41,14 @@ class Layout extends React.Component {
fontFamily: 'Montserrat, sans-serif',
marginTop: 0,
marginBottom: rhythm(-1),
minHeight: '3.5rem',
}}
>
<Link
style={{
boxShadow: 'none',
textDecoration: 'none',
color: theme.header,
color: 'rgb(255, 167, 196)',
}}
to={'/'}
>
Expand All @@ -60,6 +60,10 @@ class Layout extends React.Component {
}
render() {
const { children } = this.props;
const rootPath = `${__PATH_PREFIX__}/`;
const isHomePage = location.pathname === rootPath;
// Keep dark/light mode switch aligned between home and post page
const topPadding = isHomePage ? rhythm(1.5) : rhythm(2.15);
return (
<ThemeContext.Consumer>
{({ theme, setTheme }) => (
Expand All @@ -76,7 +80,7 @@ class Layout extends React.Component {
marginLeft: 'auto',
marginRight: 'auto',
maxWidth: rhythm(24),
padding: `${rhythm(1.5)} ${rhythm(3 / 4)}`,
padding: `${topPadding} ${rhythm(3 / 4)}`,
}}
>
<div
Expand All @@ -89,8 +93,20 @@ class Layout extends React.Component {
{this.renderHeader(theme)}
<Toggle
icons={{
checked: <img src={moon} alt="Dark Mode" />,
unchecked: <img src={sun} alt="Light Mode" />,
checked: (
<img
src={moon}
alt="Dark Mode"
style={{ pointerEvents: 'none' }}
/>
),
unchecked: (
<img
src={sun}
alt="Light Mode"
style={{ pointerEvents: 'none' }}
/>
),
}}
checked={theme.id === 'dark'}
onChange={e => setTheme(e.target.checked ? 'dark' : 'light')}
Expand Down
19 changes: 0 additions & 19 deletions src/components/Style.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,25 +164,6 @@ class Bio extends React.Component {
color: ${theme.primary.text.link}
}


.react-toggle--checked .react-toggle-thumb {
border-color: #4f5b62;
}

.react-toggle--checked .react-toggle-track {
background-color: #4f5b62;
}

.react-toggle-track {
background-color: #000;
}

.react-toggle:hover .react-toggle-track {
background-color: ${
theme.id === 'dark' ? '#37474f' : '#222'
}!important;
}

.react-toggle-track-x {
width: 17px;
height: 17px;
Expand Down
137 changes: 137 additions & 0 deletions src/components/Toggle.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
/*
* Copyright (c) 2015 instructure-react
* Forked from https://github.com/aaronshaf/react-toggle/
**/

.react-toggle {
touch-action: pan-x;

display: inline-block;
position: relative;
cursor: pointer;
background-color: transparent;
border: 0;
padding: 0;

-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;

-webkit-tap-highlight-color: rgba(0, 0, 0, 0);
-webkit-tap-highlight-color: transparent;
}

.react-toggle-screenreader-only {
border: 0;
clip: rect(0 0 0 0);
height: 1px;
margin: -1px;
overflow: hidden;
padding: 0;
position: absolute;
width: 1px;
}

.react-toggle--disabled {
cursor: not-allowed;
opacity: 0.5;
-webkit-transition: opacity 0.25s;
transition: opacity 0.25s;
}

.react-toggle-track {
width: 50px;
height: 24px;
padding: 0;
border-radius: 30px;
background-color: #222;
-webkit-transition: all 0.2s ease;
-moz-transition: all 0.2s ease;
transition: all 0.2s ease;
}

.react-toggle--checked .react-toggle-track {
background-color: #222;
}

.react-toggle-track-check {
position: absolute;
width: 14px;
height: 10px;
top: 0px;
bottom: 0px;
margin-top: auto;
margin-bottom: auto;
line-height: 0;
left: 8px;
opacity: 0;
-webkit-transition: opacity 0.25s ease;
-moz-transition: opacity 0.25s ease;
transition: opacity 0.25s ease;
}

.react-toggle--checked .react-toggle-track-check {
opacity: 1;
-webkit-transition: opacity 0.25s ease;
-moz-transition: opacity 0.25s ease;
transition: opacity 0.25s ease;
}

.react-toggle-track-x {
position: absolute;
width: 10px;
height: 10px;
top: 0px;
bottom: 0px;
margin-top: auto;
margin-bottom: auto;
line-height: 0;
right: 10px;
opacity: 1;
-webkit-transition: opacity 0.25s ease;
-moz-transition: opacity 0.25s ease;
transition: opacity 0.25s ease;
}

.react-toggle--checked .react-toggle-track-x {
opacity: 0;
}

.react-toggle-thumb {
transition: all 0.5s cubic-bezier(0.23, 1, 0.32, 1) 0ms;
position: absolute;
top: 1px;
left: 1px;
width: 22px;
height: 22px;
border-radius: 50%;
background-color: #fafafa;

-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;

-webkit-transition: all 0.25s ease;
-moz-transition: all 0.25s ease;
transition: all 0.25s ease;
}

.react-toggle--checked .react-toggle-thumb {
left: 27px;
border-color: #19ab27;
}

.react-toggle--focus .react-toggle-thumb {
-webkit-box-shadow: 0px 0px 3px 2px rgb(255, 167, 196);
-moz-box-shadow: 0px 0px 3px 2px rgb(255, 167, 196);
box-shadow: 0px 0px 2px 3px rgb(255, 167, 196);
}

.react-toggle:active:not(.react-toggle--disabled) .react-toggle-thumb {
-webkit-box-shadow: 0px 0px 5px 5px rgb(255, 167, 196);
-moz-box-shadow: 0px 0px 5px 5px rgb(255, 167, 196);
box-shadow: 0px 0px 5px 5px rgb(255, 167, 196);
}
Loading