-
Notifications
You must be signed in to change notification settings - Fork 51
🧪 [Spike] Get theming “support” in the Showcase app - First scrappy iteration #2589
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
Closed
Closed
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
8486db5
WIP
didoo 2934853
WIP2
didoo 5d10b07
WIP3
didoo a832584
WIP 4
didoo da19f32
wip 5
didoo 748e34c
WIP 6
didoo 3d5953a
WIP 7
didoo 5a5cae7
WIP 8 (dark theme for showcase)
didoo 0c6ae1c
WIP 9 (more showcase theming)
didoo c51f06c
WIP 10 (reoganized Sass/CSS file imports)
didoo 087be7c
WIP 11 (page-level showcase demos)
didoo ce01ad3
WIP 12 (replaced placeholder with custom local “component”)
didoo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| /** | ||
| * Copyright (c) HashiCorp, Inc. | ||
| * SPDX-License-Identifier: MPL-2.0 | ||
| */ | ||
|
|
||
| import Component from '@glimmer/component'; | ||
| import { on } from '@ember/modifier'; | ||
| import { action } from '@ember/object'; | ||
| import { service } from '@ember/service'; | ||
|
|
||
| import type ShwThemingService from 'showcase/services/theming'; | ||
|
|
||
| interface ShwThemeSwitcherSignature { | ||
| // Args: {}; | ||
| Element: HTMLDivElement; | ||
| } | ||
|
|
||
| export default class ShwThemeSwitcher extends Component<ShwThemeSwitcherSignature> { | ||
| @service declare readonly theming: ShwThemingService; | ||
|
|
||
| @action | ||
| onChangePageTheme(event: Event) { | ||
| const select = event.target as HTMLSelectElement; | ||
| console.log(`Theme: ${select.value}`); | ||
| console.log(`theming.getTheme: ${this.theming.getTheme()}`); | ||
|
|
||
| let theme; | ||
| let type; | ||
| switch (select.value) { | ||
| case 'light-data-attribute': | ||
| theme = 'light'; | ||
| type = 'data-attribute'; | ||
| break; | ||
| case 'light-css-class': | ||
| theme = 'light'; | ||
| type = 'css-class'; | ||
| break; | ||
| case 'dark-data-attribute': | ||
| theme = 'dark'; | ||
| type = 'data-attribute'; | ||
| break; | ||
| case 'dark-css-class': | ||
| theme = 'dark'; | ||
| type = 'css-class'; | ||
| break; | ||
| default: | ||
| theme = 'auto'; | ||
| break; | ||
| } | ||
|
|
||
| // we set the theme in the global service | ||
| this.theming.setTheme(theme, type, 'body'); | ||
| } | ||
|
|
||
| <template> | ||
| <div class="shw-theme-switcher" ...attributes> | ||
| <label | ||
| for="shw-theme-switcher-control" | ||
| class="shw-theme-switcher__label" | ||
| >Theme:</label> | ||
| <select | ||
| id="shw-theme-switcher-control" | ||
| class="shw-theme-switcher__control" | ||
| {{on "change" this.onChangePageTheme}} | ||
| > | ||
| <option value="auto">Auto (prefers-color-scheme)</option> | ||
| <option value="light-css-class">Light (CSS class)</option> | ||
| <option value="light-data-attribute">Light (data-attribute)</option> | ||
| <option value="dark-css-class">Dark (CSS class)</option> | ||
| <option value="dark-data-attribute">Dark (data-attribute)</option> | ||
| </select> | ||
| </div> | ||
| </template> | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| /** | ||
| * Copyright (c) HashiCorp, Inc. | ||
| * SPDX-License-Identifier: MPL-2.0 | ||
| */ | ||
|
|
||
| import Controller from '@ember/controller'; | ||
| import { inject as service } from '@ember/service'; | ||
|
|
||
| export default class ThemingController extends Controller { | ||
| @service theming; | ||
|
|
||
| get currentTheme() { | ||
| return this.theming.getTheme(); | ||
| } | ||
|
|
||
| get inverseTheme() { | ||
| return this.theming.getInverseTheme(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| import Service from '@ember/service'; | ||
| import { tracked } from '@glimmer/tracking'; | ||
|
|
||
| export default class ShwThemingService extends Service { | ||
| // undefined means to use the default in the browser, which is "prefers-color-scheme" | ||
| @tracked currentTheme = 'auto'; | ||
|
|
||
| getTheme() { | ||
| return this.currentTheme; | ||
| // return localStorage.getItem('shwCurrentTheme'); | ||
| } | ||
|
|
||
| getInverseTheme() { | ||
| // TODO what happens if we have four themes, like IBM, instead of just two? | ||
| return this.currentTheme === 'dark' ? 'light' : 'dark'; | ||
| } | ||
|
|
||
| setTheme(theme, type, target) { | ||
| // const fullKey = this.#buildKey(key, appName); | ||
| this.currentTheme = theme; | ||
| // localStorage.setItem('shwCurrentTheme', theme); | ||
| console.log('setting theme', theme, type, target); | ||
|
|
||
| const bodyElement = document.querySelector('body'); | ||
| bodyElement.removeAttribute('data-shw-theme'); | ||
| bodyElement.classList.remove('shw-theme-dark'); | ||
| bodyElement.classList.remove('shw-theme-light'); | ||
|
|
||
| if (type === 'data-attribute') { | ||
| bodyElement.setAttribute('data-shw-theme', theme); | ||
| } else if (type === 'css-class') { | ||
| bodyElement.classList.add(`shw-theme-${theme}`); | ||
| } | ||
| // } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
showcase/app/styles/showcase-components/theme-switcher.scss
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| /** | ||
| * Copyright (c) HashiCorp, Inc. | ||
| * SPDX-License-Identifier: MPL-2.0 | ||
| */ | ||
|
|
||
| .shw-theme-switcher { | ||
| display: flex; | ||
| gap: 8px; | ||
| align-items: center; | ||
| } | ||
|
|
||
| .shw-theme-switcher__label { | ||
| color: var(--shw-color-black); | ||
| font-size: 0.75rem; | ||
| font-family: monaco, Consolas, "Lucida Console", monospace; | ||
| } | ||
|
|
||
| .shw-theme-switcher__control { | ||
| height: 24px; | ||
| padding: 2px 24px 2px 8px; | ||
| color: var(--shw-color-gray-100); | ||
| font-size: 0.75rem; | ||
| font-family: monaco, Consolas, "Lucida Console", monospace; | ||
| background-color: var(--shw-color-gray-600); | ||
| background-image: url("data:image/svg+xml,%3Csvg viewBox='0 0 24 24' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='M3.34572 7H20.6543C21.8517 7 22.4504 8.4463 21.6028 9.29391L12.9519 17.9515C12.4272 18.4763 11.5728 18.4763 11.0481 17.9515L2.39722 9.29391C1.54961 8.4463 2.14832 7 3.34572 7Z' fill='%23808080'/%3E%3C/svg%3E"); // notice: the 'caret' color is hardcoded here! | ||
| background-repeat: no-repeat; | ||
| background-position: right 6px top 4px; | ||
| background-size: 12px 12px; | ||
| border: 1px solid var(--shw-color-gray-400); | ||
| border-radius: 3px; | ||
| appearance: none; | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
in
.gtsfiles@actioncan be replaced with an arrow function