Skip to content

Commit

Permalink
feat(theme): add switch to select light/dark mode when the default th…
Browse files Browse the repository at this point in the history
…eme is auto
  • Loading branch information
Joxit committed Mar 8, 2023
1 parent 2dce587 commit 8c3189b
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"node-sass": "^8.0.0",
"prettier": "^2.8.1",
"riot": "^7.1.0",
"riot-mui": "github:joxit/riot-5-mui#a9b0ce4",
"riot-mui": "github:joxit/riot-5-mui#b892806",
"rollup": "^3.9.0",
"rollup-plugin-app-utils": "^1.0.6",
"rollup-plugin-copy": "^3.4.0",
Expand Down
11 changes: 10 additions & 1 deletion src/components/docker-registry-ui.riot
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
<li>
<a href="https://github.com/Joxit/docker-registry-ui/blob/main/LICENSE">License AGPL-3.0</a>
</li>
<li if="{ props.theme === 'auto' }">
<material-switch on-change="{ onThemeChange }" checked="{ state.themeSwitch }"></material-switch>
</li>
</ul>
</material-footer>
</footer>
Expand Down Expand Up @@ -156,7 +159,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
this.state.catalogElementsLimit = props.catalogElementsLimit || 100000;
this.state.pullUrl = this.pullUrl(this.state.registryUrl, props.pullUrl);
this.state.useControlCacheHeader = props.useControlCacheHeader;
loadTheme(props, this.root.parentNode.style);
const theme = loadTheme(props, this.root.parentNode.style);
this.state.themeSwitch = theme === 'dark';
},
onServerChange(registryUrl) {
this.update({
Expand Down Expand Up @@ -216,6 +220,11 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
filter: value,
});
},
onThemeChange(e) {
const theme = e.target.checked ? 'dark' : 'light';
loadTheme({ ...this.props, theme }, this.root.parentNode.style);
this.update({ themeSwitch: e.target.checked });
},
baseRoute: '([^#]*?)/(\\?[^#]*?)?(#!)?(/?)',
router,
version,
Expand Down
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
MaterialDropdown,
MaterialPopup,
MaterialInput,
MaterialSwitch,
} from 'riot-mui';

import DockerRegistryUI from './components/docker-registry-ui.riot';
Expand All @@ -31,6 +32,7 @@ register('material-tabs', MaterialTabs);
register('material-dropdown', MaterialDropdown);
register('material-popup', MaterialPopup);
register('material-input', MaterialInput);
register('material-switch', MaterialSwitch);

const createApp = component(DockerRegistryUI);
const tags = document.getElementsByTagName('docker-registry-ui');
Expand Down
4 changes: 3 additions & 1 deletion src/scripts/theme.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,12 @@ const preferDarkMode = ({ theme }) => {
};

export const loadTheme = (props, style) => {
THEME = preferDarkMode(props) ? DARK_THEME : LIGHT_THEME;
const isDarkMode = preferDarkMode(props);
THEME = isDarkMode ? { ...DARK_THEME } : { ...LIGHT_THEME };
Object.entries(props)
.filter(([k, v]) => v && /^theme[A-Z]/.test(k))
.map(([k, v]) => [normalizeKey(k), v])
.forEach(([k, v]) => (THEME[k] = v));
Object.entries(THEME).forEach(([k, v]) => style.setProperty(`--${k}`, v));
return isDarkMode ? 'dark' : 'light';
};
1 change: 1 addition & 0 deletions src/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
@import 'riot-mui/src/material-elements/material-dropdown/material-dropdown.scss';
@import 'riot-mui/src/material-elements/material-popup/material-popup.scss';
@import 'riot-mui/src/material-elements/material-input/material-input.scss';
@import 'riot-mui/src/material-elements/material-switch/material-switch.scss';

@import './roboto.scss';
@import './material-icons.scss';
Expand Down

0 comments on commit 8c3189b

Please sign in to comment.