Skip to content

Commit cf942a9

Browse files
committed
added Shw::ThemeSwitcher component for showcase
1 parent e0667f4 commit cf942a9

File tree

3 files changed

+90
-0
lines changed

3 files changed

+90
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/**
2+
* Copyright (c) HashiCorp, Inc.
3+
* SPDX-License-Identifier: MPL-2.0
4+
*/
5+
6+
import Component from '@glimmer/component';
7+
import { on } from '@ember/modifier';
8+
import { action } from '@ember/object';
9+
import { service } from '@ember/service';
10+
import { eq } from 'ember-truth-helpers';
11+
// import { tracked } from '@glimmer/tracking';
12+
13+
import type HdsThemingService from '@hashicorp/design-system-components/services/hds-theming.ts';
14+
15+
interface ShwThemeSwitcherSignature {
16+
Element: HTMLDivElement;
17+
}
18+
19+
const options = {
20+
none: 'None (No theming)',
21+
system: 'System (prefers-color-scheme)',
22+
light: 'Light (data-attribute)',
23+
dark: 'Dark (data-attribute)',
24+
};
25+
26+
export default class ShwThemeSwitcher extends Component<ShwThemeSwitcherSignature> {
27+
@service declare readonly hdsTheming: HdsThemingService;
28+
29+
@action
30+
onChangePageTheme(event: Event) {
31+
const select = event.target as HTMLSelectElement;
32+
33+
// we set the theme in the global service
34+
this.hdsTheming.setTheme(select.value);
35+
}
36+
37+
<template>
38+
<div class="shw-theme-switcher" ...attributes>
39+
<label
40+
for="shw-theme-switcher-control"
41+
class="shw-theme-switcher__label"
42+
>Theme:</label>
43+
<select
44+
id="shw-theme-switcher-control"
45+
class="shw-theme-switcher__control"
46+
{{on "change" this.onChangePageTheme}}
47+
>
48+
{{#each-in options as |key label|}}
49+
<option
50+
value={{key}}
51+
selected={{eq this.hdsTheming.currentTheme key}}
52+
>{{label}}</option>
53+
{{/each-in}}
54+
</select>
55+
</div>
56+
</template>
57+
}

showcase/app/styles/app.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
@use "./showcase-components/label";
2525
@use "./showcase-components/outliner";
2626
@use "./showcase-components/placeholder";
27+
@use "./showcase-components/theme-switcher";
2728
@use "./mock-components/app";
2829
@use "./mock-components/demo/breakpoints";
2930

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Copyright (c) HashiCorp, Inc.
3+
* SPDX-License-Identifier: MPL-2.0
4+
*/
5+
6+
.shw-theme-switcher {
7+
display: flex;
8+
gap: 8px;
9+
align-items: center;
10+
}
11+
12+
.shw-theme-switcher__label {
13+
color: var(--shw-color-black);
14+
font-size: 0.75rem;
15+
font-family: monaco, Consolas, "Lucida Console", monospace;
16+
}
17+
18+
.shw-theme-switcher__control {
19+
height: 24px;
20+
padding: 2px 24px 2px 8px;
21+
color: var(--shw-color-gray-100);
22+
font-size: 0.75rem;
23+
font-family: monaco, Consolas, "Lucida Console", monospace;
24+
background-color: var(--shw-color-gray-600);
25+
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!
26+
background-repeat: no-repeat;
27+
background-position: right 6px top 4px;
28+
background-size: 12px 12px;
29+
border: 1px solid var(--shw-color-gray-400);
30+
border-radius: 3px;
31+
appearance: none;
32+
}

0 commit comments

Comments
 (0)