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

Theme Switcher Component #97

Merged
merged 3 commits into from
Jun 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 5 additions & 0 deletions .changeset/mean-bikes-laugh.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"mx-ui-components": minor
---

Add Theme Switcher Component
25 changes: 25 additions & 0 deletions addon/components/mox/theme-switch.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<div class="mox-theme-switch" ...attributes>
<div class="flex items-center space-x-2 relative {{if this.isChecked "text-gray-800"}}">
<input
id={{this.fieldId}}
aria-label={{this.label}}
type="checkbox"
class="transform appearance-none outline-none
flex items-center rounded-full p-1 cursor-pointer transition"
checked={{this.isChecked}}
{{on 'change' (fn this.switchTheme this.otherTheme)}}
data-test-mox-theme-switch
/>
<div class="mox-theme-switch-icon pointer-events-none {{if this.isChecked "is-checked"}}">
{{#if (eq this.currentTheme "dark")}}
<div class="text-white" data-test-mox-theme-switch-current="dark">
{{yield to="dark-icon"}}
</div>
{{else}}
<div class="text-gray-900" data-test-mox-theme-switch-current="light">
{{yield to="light-icon"}}
</div>
{{/if}}
</div>
</div>
</div>
42 changes: 42 additions & 0 deletions addon/components/mox/theme-switch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import Component from '@glimmer/component';
import { action } from '@ember/object';
import { dasherize } from '@ember/string';
import { tracked } from '@glimmer/tracking';

export default class MoxThemeSwitchComponent extends Component {
@tracked
isChecked = false;

allThemes = ['dark', 'light'];

constructor() {
super(...arguments);
this.isChecked = this.args.isChecked || false;
}

get currentTheme() {
return this.isChecked ? 'dark' : 'light';
}

get otherTheme() {
return this.allThemes.filter(theme => theme !== this.currentTheme)[0];
}

get label() {
if (this.args.label) {
return this.args.label;
}

return `Use ${this.otherTheme} mode`;
}

get fieldId() {
return this.args.id ? this.args.id : dasherize(this.label);
}

@action
switchTheme(newTheme, event) {
this.isChecked = !this.isChecked;
this.args.toggleAction(newTheme, event);
}
}
55 changes: 53 additions & 2 deletions addon/styles/mx-ui-components.css
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
.mxa-toggle input:checked,
.mox-toggle input:checked {
.mox-toggle input:checked,
.mox-theme-switch input:checked {
background-image: none;
}

.mxa-toggle input:after,
.mox-toggle input:after {
.mox-toggle input:after,
.mox-theme-switch input:after {
content: "";
height: 20px;
width: 20px;
Expand Down Expand Up @@ -38,6 +40,55 @@
left: 2px;
}

.mox-theme-switch input {
height: 1.5rem;
width: 2.5rem;
outline: 2px solid #e5e7eb; /* bg-gray-200 */
background-color: #e5e7eb; /* bg-gray-200 */
outline-offset: 0;
border: none;
}

.mox-theme-switch input:checked {
background-color: #1f2937; /* bg-gray-800 */
outline: 2px solid #1f2937; /* bg-gray-800 */
color: #1f2937; /* bg-gray-800 */
}

.mox-theme-switch input:focus {
outline: 2px solid #06B6D4;
box-shadow: none;
outline-offset: 0;
}

.mox-theme-switch input:after {
height: 18px;
width: 18px;
top: 3px;
left: 3px;
}

.mox-theme-switch-icon {
position: absolute;
left: -4px;
top: 4px;
right: auto;
display: block;
outline: none;
transform: translateX(0);
transition: transform 0.15s;
transition-timing-function: ease-in-out;
}

.mox-theme-switch input:checked:after,
.mox-theme-switch-icon.is-checked {
transform: translateX(15px);
}

.mox-theme-switch input:checked:after {
background-color: transparent;
}

.mxa-toggle input:checked:after,
.mox-toggle input:checked:after {
transform: translateX(20px);
Expand Down
1 change: 1 addition & 0 deletions app/components/mox/theme-switch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from 'mx-ui-components/components/mox/theme-switch';
2 changes: 1 addition & 1 deletion stories/mox-accordion.stories.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { hbs } from 'ember-cli-htmlbars';

export default {
title: 'Mox/Mox::Accordion',
title: 'Mox Dark/Mox::Accordion',
parameters: {
backgrounds: {
default: 'Dark',
Expand Down
2 changes: 1 addition & 1 deletion stories/mox-badge.stories.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { hbs } from 'ember-cli-htmlbars';

export default {
title: 'Mox/Mox::Badge',
title: 'Mox Dark/Mox::Badge',
parameters: {
backgrounds: {
default: 'Dark',
Expand Down
2 changes: 1 addition & 1 deletion stories/mox-button.stories.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { hbs } from 'ember-cli-htmlbars';

export default {
title: 'Mox/Mox::Button',
title: 'Mox Dark/Mox::Button',
argTypes: {
children: { control: 'text' },
buttonType: { control: 'text' },
Expand Down
2 changes: 1 addition & 1 deletion stories/mox-card.stories.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { hbs } from 'ember-cli-htmlbars';

export default {
title: 'Mox/Mox::Card',
title: 'Mox Dark/Mox::Card',
argTypes: {
title: { control: 'text' },
subtitle: { control: 'text' },
Expand Down
2 changes: 1 addition & 1 deletion stories/mox-code-block.stories.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { hbs } from 'ember-cli-htmlbars';

export default {
title: 'Mox/Mox::CodeBlock',
title: 'Mox Dark/Mox::CodeBlock',
parameters: {
backgrounds: {
default: 'Dark',
Expand Down
2 changes: 1 addition & 1 deletion stories/mox-confirm-modal.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { hbs } from 'ember-cli-htmlbars';
import { action } from '@storybook/addon-actions';

export default {
title: 'Mox/Mox::ConfirmModal',
title: 'Mox Dark/Mox::ConfirmModal',
};

const actionsData = {
Expand Down
2 changes: 1 addition & 1 deletion stories/mox-form-field.stories.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { hbs } from 'ember-cli-htmlbars';

export default {
title: 'Mox/Mox::FormField',
title: 'Mox Dark/Mox::FormField',
parameters: {
backgrounds: {
default: 'Dark',
Expand Down
2 changes: 1 addition & 1 deletion stories/mox-global-header.stories.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { hbs } from 'ember-cli-htmlbars';

export default {
title: 'Mox/Mox::GlobalHeader',
title: 'Mox Dark/Mox::GlobalHeader',
parameters: {
backgrounds: {
default: 'Dark',
Expand Down
2 changes: 1 addition & 1 deletion stories/mox-icon.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { hbs } from 'ember-cli-htmlbars';
let colorOptions = ['text-white', 'text-gray-300', 'text-red-500', 'text-cyan-500'];

export default {
title: 'Mox/Mox::Icon',
title: 'Mox Dark/Mox::Icon',
argTypes: {
color: {
control: {
Expand Down
2 changes: 1 addition & 1 deletion stories/mox-input.stories.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { hbs } from 'ember-cli-htmlbars';

export default {
title: 'Mox/Mox::Input',
title: 'Mox Dark/Mox::Input',
parameters: {
backgrounds: {
default: 'Dark',
Expand Down
2 changes: 1 addition & 1 deletion stories/mox-label.stories.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { hbs } from 'ember-cli-htmlbars';

export default {
title: 'Mox/Mox::Label',
title: 'Mox Dark/Mox::Label',
parameters: {
backgrounds: {
default: 'Dark',
Expand Down
2 changes: 1 addition & 1 deletion stories/mox-link.stories.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { hbs } from 'ember-cli-htmlbars';

export default {
title: 'Mox/Mox::Link',
title: 'Mox Dark/Mox::Link',
parameters: {
backgrounds: {
default: 'Dark',
Expand Down
2 changes: 1 addition & 1 deletion stories/mox-list.stories.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { hbs } from 'ember-cli-htmlbars';

export default {
title: 'Mox/Mox::List',
title: 'Mox Dark/Mox::List',
parameters: {
backgrounds: {
default: 'Dark',
Expand Down
2 changes: 1 addition & 1 deletion stories/mox-modal-dialog.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { hbs } from 'ember-cli-htmlbars';
import { action } from '@storybook/addon-actions';

export default {
title: 'Mox/Mox::ModalDialog',
title: 'Mox Dark/Mox::ModalDialog',
};

const actionsData = {
Expand Down
2 changes: 1 addition & 1 deletion stories/mox-modal.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { hbs } from 'ember-cli-htmlbars';
import { action } from '@storybook/addon-actions';

export default {
title: 'Mox/Mox::Modal',
title: 'Mox Dark/Mox::Modal',
};

const actionsData = {
Expand Down
2 changes: 1 addition & 1 deletion stories/mox-navigation.stories.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { hbs } from 'ember-cli-htmlbars';

export default {
title: 'Mox/Mox::Navigation',
title: 'Mox Dark/Mox::Navigation',
parameters: {
backgrounds: {
default: 'Dark',
Expand Down
2 changes: 1 addition & 1 deletion stories/mox-search-input.stories.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { hbs } from 'ember-cli-htmlbars';

export default {
title: 'Mox/Mox::SearchInput',
title: 'Mox Dark/Mox::SearchInput',
parameters: {
backgrounds: {
default: 'Dark',
Expand Down
2 changes: 1 addition & 1 deletion stories/mox-select.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const resourceTypes = [
];

export default {
title: 'Mox/Mox::Select',
title: 'Mox Dark/Mox::Select',
parameters: {
backgrounds: {
default: 'Dark',
Expand Down
2 changes: 1 addition & 1 deletion stories/mox-tabs.stories.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { hbs } from 'ember-cli-htmlbars';

export default {
title: 'Mox/Mox::Tabs',
title: 'Mox Dark/Mox::Tabs',
parameters: {
backgrounds: {
default: 'Dark',
Expand Down
2 changes: 1 addition & 1 deletion stories/mox-text-area.stories.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { hbs } from 'ember-cli-htmlbars';

export default {
title: 'Mox/Mox::TextArea',
title: 'Mox Dark/Mox::TextArea',
parameters: {
backgrounds: {
default: 'Dark',
Expand Down
35 changes: 35 additions & 0 deletions stories/mox-theme-switch.stories.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { hbs } from 'ember-cli-htmlbars';

export default {
title: 'Mox Dark/Mox::ThemeSwitch',
parameters: {
backgrounds: {
default: 'Dark',
},
},
};

const Template = (args) => ({
template: hbs`
<Mox::ThemeSwitch @isChecked={{this.isChecked}}>
<:light-icon>
<Mox::Icon @iconName="sun" @size="small" />
</:light-icon>
<:dark-icon>
<Mox::Icon @iconName="moon" @size="small" />
</:dark-icon>
</Mox::ThemeSwitch>`,
context: args,
});

export const DefaultTheme = Template.bind({});
DefaultTheme.args = {
message: '',
isChecked: false,
};

export const Checked = Template.bind({});
Checked.args = {
message: '',
isChecked: true,
};
2 changes: 1 addition & 1 deletion stories/mox-toast.stories.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { hbs } from 'ember-cli-htmlbars';

export default {
title: 'Mox/Mox::Toast',
title: 'Mox Dark/Mox::Toast',
parameters: {
backgrounds: {
default: 'Dark',
Expand Down
2 changes: 1 addition & 1 deletion stories/mox-toggle.stories.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { hbs } from 'ember-cli-htmlbars';

export default {
title: 'Mox/Mox::Toggle',
title: 'Mox Dark/Mox::Toggle',
parameters: {
backgrounds: {
default: 'Dark',
Expand Down
2 changes: 1 addition & 1 deletion stories/mox-typeahead-select.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const options = [
];

export default {
title: 'Mox/Mox::TypeaheadSelect',
title: 'Mox Dark/Mox::TypeaheadSelect',
argTypes: {
options: { control: 'text' },
buttonType: { control: 'text' },
Expand Down
2 changes: 1 addition & 1 deletion stories/mox-upload.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { hbs } from 'ember-cli-htmlbars';
import { action } from '@ember/object';

export default {
title: 'Mox/Mox::Upload',
title: 'Mox Dark/Mox::Upload',
parameters: {
backgrounds: {
default: 'Dark',
Expand Down
Loading