Skip to content

Commit

Permalink
feat(rtl): add rtl support (#310)
Browse files Browse the repository at this point in the history
  • Loading branch information
piitaya authored Apr 14, 2022
1 parent 7c09335 commit de7f52a
Show file tree
Hide file tree
Showing 28 changed files with 201 additions and 62 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
ActionHandlerEvent,
computeRTL,
handleAction,
hasAction,
HomeAssistant,
Expand Down Expand Up @@ -188,10 +189,13 @@ export class AlarmControlPanelCard extends LitElement implements LovelaceCard {
"--shape-color": `rgba(${color}, 0.2)`,
};

const rtl = computeRTL(this.hass);

return html`
<ha-card>
<mushroom-card .layout=${layout} no-card-style>
<mushroom-card .layout=${layout} no-card-style ?rtl=${rtl}>
<mushroom-state-item
?rtl=${rtl}
.layout=${layout}
@action=${this._handleAction}
.actionHandler=${actionHandler({
Expand Down Expand Up @@ -224,7 +228,7 @@ export class AlarmControlPanelCard extends LitElement implements LovelaceCard {
</mushroom-state-item>
${actions.length > 0
? html`
<mushroom-button-group .fill="${layout !== "horizontal"},">
<mushroom-button-group .fill="${layout !== "horizontal"}" ?rtl=${rtl}>
${actions.map(
(action) => html`
<mushroom-button
Expand Down
9 changes: 8 additions & 1 deletion src/cards/chips-card/chips-card.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
computeRTL,
HomeAssistant,
LovelaceCard,
LovelaceCardConfig,
Expand Down Expand Up @@ -70,8 +71,10 @@ export class ChipsCard extends LitElement implements LovelaceCard {
alignment = `align-${this._config.alignment}`;
}

const rtl = computeRTL(this._hass);

return html`
<div class="chip-container ${alignment}">
<div class="chip-container ${alignment}" ?rtl=${rtl}>
${this._config.chips.map((chip) => this.renderChip(chip))}
</div>
`;
Expand Down Expand Up @@ -115,6 +118,10 @@ export class ChipsCard extends LitElement implements LovelaceCard {
.chip-container *:not(:last-child) {
margin-right: var(--chip-spacing);
}
.chip-container[rtl] *:not(:last-child) {
margin-right: initial;
margin-left: var(--chip-spacing);
}
`,
];
}
Expand Down
11 changes: 10 additions & 1 deletion src/cards/chips-card/chips/action-chip.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { ActionHandlerEvent, handleAction, hasAction, HomeAssistant } from "custom-card-helpers";
import {
ActionHandlerEvent,
computeRTL,
handleAction,
hasAction,
HomeAssistant,
} from "custom-card-helpers";
import { css, CSSResultGroup, html, LitElement, TemplateResult } from "lit";
import { customElement, property, state } from "lit/decorators.js";
import { styleMap } from "lit/directives/style-map.js";
Expand Down Expand Up @@ -54,8 +60,11 @@ export class ActionChip extends LitElement implements LovelaceChip {
iconStyle["--color"] = `rgb(${iconRgbColor})`;
}

const rtl = computeRTL(this.hass);

return html`
<mushroom-chip
?rtl=${rtl}
@action=${this._handleAction}
.actionHandler=${actionHandler({
hasHold: hasAction(this._config.hold_action),
Expand Down
11 changes: 10 additions & 1 deletion src/cards/chips-card/chips/alarm-control-panel-chip.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { ActionHandlerEvent, handleAction, hasAction, HomeAssistant } from "custom-card-helpers";
import {
ActionHandlerEvent,
computeRTL,
handleAction,
hasAction,
HomeAssistant,
} from "custom-card-helpers";
import { css, CSSResultGroup, html, LitElement, TemplateResult } from "lit";
import { customElement, property, state } from "lit/decorators.js";
import { classMap } from "lit/directives/class-map.js";
Expand Down Expand Up @@ -83,8 +89,11 @@ export class AlarmControlPanelChip extends LitElement implements LovelaceChip {
this.hass
);

const rtl = computeRTL(this.hass);

return html`
<mushroom-chip
?rtl=${rtl}
@action=${this._handleAction}
.actionHandler=${actionHandler({
hasHold: hasAction(this._config.hold_action),
Expand Down
10 changes: 8 additions & 2 deletions src/cards/chips-card/chips/back-chip.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { HomeAssistant } from "custom-card-helpers";
import { computeRTL, HomeAssistant } from "custom-card-helpers";
import { css, CSSResultGroup, html, LitElement, TemplateResult } from "lit";
import { customElement, property, state } from "lit/decorators.js";
import { actionHandler } from "../../../utils/directives/action-handler-directive";
Expand Down Expand Up @@ -43,8 +43,14 @@ export class BackChip extends LitElement implements LovelaceChip {

const icon = this._config.icon || DEFAULT_BACK_ICON;

const rtl = computeRTL(this.hass);

return html`
<mushroom-chip @action=${this._handleAction} .actionHandler=${actionHandler()}>
<mushroom-chip
?rtl=${rtl}
@action=${this._handleAction}
.actionHandler=${actionHandler()}
>
<ha-icon .icon=${icon}></ha-icon>
</mushroom-chip>
`;
Expand Down
20 changes: 14 additions & 6 deletions src/cards/chips-card/chips/conditional-chip-editor.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import type { MDCTabBarActivatedEvent } from "@material/tab-bar";
import { fireEvent, HASSDomEvent, HomeAssistant, LovelaceConfig } from "custom-card-helpers";
import {
computeRTL,
fireEvent,
HASSDomEvent,
HomeAssistant,
LovelaceConfig,
} from "custom-card-helpers";
import { css, CSSResultGroup, html, LitElement, TemplateResult } from "lit";
import { customElement, property, query, state } from "lit/decorators.js";
import setupCustomlocalize from "../../../localize";
Expand Down Expand Up @@ -49,6 +55,8 @@ export class ConditionalChipEditor extends LitElement implements LovelaceChipEdi

const customLocalize = setupCustomlocalize(this.hass);

const rtl = computeRTL(this.hass);

return html`
<mwc-tab-bar
.activeIndex=${this._cardTab ? 1 : 0}
Expand Down Expand Up @@ -121,7 +129,7 @@ export class ConditionalChipEditor extends LitElement implements LovelaceChipEdi
)}
${this._config.conditions.map(
(cond, idx) => html`
<div class="condition">
<div class="condition" ?rtl=${rtl}>
<div class="entity">
<ha-entity-picker
.hass=${this.hass}
Expand Down Expand Up @@ -329,6 +337,10 @@ export class ConditionalChipEditor extends LitElement implements LovelaceChipEdi
.condition .state mushroom-select {
margin-right: 16px;
}
.condition[rtl] .state mushroom-select {
margin-right: initial;
margin-left: 16px;
}
.card {
margin-top: 8px;
border: 1px solid var(--divider-color);
Expand Down Expand Up @@ -356,7 +368,3 @@ export class ConditionalChipEditor extends LitElement implements LovelaceChipEdi
];
}
}

function capitalizeFirstLetter(string: string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
11 changes: 10 additions & 1 deletion src/cards/chips-card/chips/entity-chip.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { ActionHandlerEvent, handleAction, hasAction, HomeAssistant } from "custom-card-helpers";
import {
ActionHandlerEvent,
computeRTL,
handleAction,
hasAction,
HomeAssistant,
} from "custom-card-helpers";
import { css, CSSResultGroup, html, LitElement, TemplateResult } from "lit";
import { customElement, property, state } from "lit/decorators.js";
import { classMap } from "lit/directives/class-map.js";
Expand Down Expand Up @@ -75,8 +81,11 @@ export class EntityChip extends LitElement implements LovelaceChip {
this.hass
);

const rtl = computeRTL(this.hass);

return html`
<mushroom-chip
?rtl=${rtl}
@action=${this._handleAction}
.actionHandler=${actionHandler({
hasHold: hasAction(this._config.hold_action),
Expand Down
11 changes: 10 additions & 1 deletion src/cards/chips-card/chips/light-chip.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { ActionHandlerEvent, handleAction, hasAction, HomeAssistant } from "custom-card-helpers";
import {
ActionHandlerEvent,
computeRTL,
handleAction,
hasAction,
HomeAssistant,
} from "custom-card-helpers";
import { css, CSSResultGroup, html, LitElement, TemplateResult } from "lit";
import { customElement, property, state } from "lit/decorators.js";
import { classMap } from "lit/directives/class-map.js";
Expand Down Expand Up @@ -91,8 +97,11 @@ export class LightChip extends LitElement implements LovelaceChip {
this.hass
);

const rtl = computeRTL(this.hass);

return html`
<mushroom-chip
?rtl=${rtl}
@action=${this._handleAction}
.actionHandler=${actionHandler({
hasHold: hasAction(this._config.hold_action),
Expand Down
10 changes: 8 additions & 2 deletions src/cards/chips-card/chips/menu-chip.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { fireEvent, HomeAssistant } from "custom-card-helpers";
import { computeRTL, fireEvent, HomeAssistant } from "custom-card-helpers";
import { css, CSSResultGroup, html, LitElement, TemplateResult } from "lit";
import { customElement, property, state } from "lit/decorators.js";
import { actionHandler } from "../../../utils/directives/action-handler-directive";
Expand Down Expand Up @@ -43,8 +43,14 @@ export class MenuChip extends LitElement implements LovelaceChip {

const icon = this._config.icon || DEFAULT_MENU_ICON;

const rtl = computeRTL(this.hass);

return html`
<mushroom-chip @action=${this._handleAction} .actionHandler=${actionHandler()}>
<mushroom-chip
?rtl=${rtl}
@action=${this._handleAction}
.actionHandler=${actionHandler()}
>
<ha-icon .icon=${icon}></ha-icon>
</mushroom-chip>
`;
Expand Down
11 changes: 10 additions & 1 deletion src/cards/chips-card/chips/template-chip.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { ActionHandlerEvent, handleAction, hasAction, HomeAssistant } from "custom-card-helpers";
import {
ActionHandlerEvent,
computeRTL,
handleAction,
hasAction,
HomeAssistant,
} from "custom-card-helpers";
import { UnsubscribeFunc } from "home-assistant-js-websocket";
import { css, CSSResultGroup, html, LitElement, PropertyValues, TemplateResult } from "lit";
import { customElement, property, state } from "lit/decorators.js";
Expand Down Expand Up @@ -92,8 +98,11 @@ export class TemplateChip extends LitElement implements LovelaceChip {
const iconColor = this.getValue("icon_color");
const content = this.getValue("content");

const rtl = computeRTL(this.hass);

return html`
<mushroom-chip
?rtl=${rtl}
@action=${this._handleAction}
.actionHandler=${actionHandler({
hasHold: hasAction(this._config.hold_action),
Expand Down
4 changes: 4 additions & 0 deletions src/cards/chips-card/chips/weather-chip.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
ActionHandlerEvent,
computeRTL,
formatNumber,
handleAction,
hasAction,
Expand Down Expand Up @@ -72,8 +73,11 @@ export class WeatherChip extends LitElement implements LovelaceChip {
displayLabels.push(temperatureDisplay);
}

const rtl = computeRTL(this.hass);

return html`
<mushroom-chip
?rtl=${rtl}
@action=${this._handleAction}
.actionHandler=${actionHandler({
hasHold: hasAction(this._config.hold_action),
Expand Down
6 changes: 4 additions & 2 deletions src/cards/cover-card/controls/cover-buttons-control.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { HomeAssistant } from "custom-card-helpers";
import { computeRTL, HomeAssistant } from "custom-card-helpers";
import { css, CSSResultGroup, html, LitElement, TemplateResult } from "lit";
import { customElement, property } from "lit/decorators.js";
import { classMap } from "lit/directives/class-map.js";
Expand Down Expand Up @@ -58,8 +58,10 @@ export class CoverButtonsControl extends LitElement {
}

protected render(): TemplateResult {
const rtl = computeRTL(this.hass);

return html`
<mushroom-button-group .fill=${this.fill}>
<mushroom-button-group .fill=${this.fill} ?rtl=${rtl}>
${supportsFeature(this.entity, COVER_SUPPORT_CLOSE)
? html`
<mushroom-button
Expand Down
8 changes: 6 additions & 2 deletions src/cards/cover-card/cover-card.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
ActionHandlerEvent,
computeRTL,
handleAction,
hasAction,
HomeAssistant,
Expand Down Expand Up @@ -160,9 +161,12 @@ export class CoverCard extends LitElement implements LovelaceCard {
stateValue += ` - ${this.position}%`;
}

const rtl = computeRTL(this.hass);

return html`
<mushroom-card .layout=${layout}>
<mushroom-card .layout=${layout} ?rtl=${rtl}>
<mushroom-state-item
?rtl=${rtl}
.layout=${layout}
@action=${this._handleAction}
.actionHandler=${actionHandler({
Expand Down Expand Up @@ -192,7 +196,7 @@ export class CoverCard extends LitElement implements LovelaceCard {
</mushroom-state-item>
${this._controls.length > 0
? html`
<div class="actions">
<div class="actions" ?rtl=${rtl}>
${this.renderActiveControl(entity, layout)}
${this.renderNextControlButton()}
</div>
Expand Down
6 changes: 5 additions & 1 deletion src/cards/entity-card/entity-card.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
ActionHandlerEvent,
computeRTL,
handleAction,
hasAction,
HomeAssistant,
Expand Down Expand Up @@ -106,9 +107,12 @@ export class EntityCard extends LitElement implements LovelaceCard {

const iconColor = this._config.icon_color;

const rtl = computeRTL(this.hass);

return html`
<mushroom-card .layout=${layout}>
<mushroom-card .layout=${layout} ?rtl=${rtl}>
<mushroom-state-item
?rtl=${rtl}
.layout=${layout}
@action=${this._handleAction}
.actionHandler=${actionHandler({
Expand Down
8 changes: 6 additions & 2 deletions src/cards/fan-card/fan-card.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
ActionHandlerEvent,
computeRTL,
handleAction,
hasAction,
HomeAssistant,
Expand Down Expand Up @@ -139,9 +140,12 @@ export class FanCard extends LitElement implements LovelaceCard {
stateValue += ` - ${this.percentage}%`;
}

const rtl = computeRTL(this.hass);

return html`
<mushroom-card .layout=${layout}>
<mushroom-card .layout=${layout} ?rtl=${rtl}>
<mushroom-state-item
?rtl=${rtl}
.layout=${layout}
@action=${this._handleAction}
.actionHandler=${actionHandler({
Expand Down Expand Up @@ -175,7 +179,7 @@ export class FanCard extends LitElement implements LovelaceCard {
</mushroom-state-item>
${this._config.show_percentage_control || this._config.show_oscillate_control
? html`
<div class="actions">
<div class="actions" ?rtl=${rtl}>
${this._config.show_percentage_control
? html`
<mushroom-fan-percentage-control
Expand Down
Loading

0 comments on commit de7f52a

Please sign in to comment.