From 377c09aa9a10eb3a1a7b9d12638389f052a19bdc Mon Sep 17 00:00:00 2001 From: Alex Andreev Date: Tue, 13 Jul 2021 13:20:46 +0300 Subject: [PATCH] Convert Badge to CSS Modules Signed-off-by: Alex Andreev --- .../badge/{badge.scss => badge.module.css} | 66 ++++++++++--------- src/renderer/components/badge/badge.tsx | 35 ++++++---- 2 files changed, 58 insertions(+), 43 deletions(-) rename src/renderer/components/badge/{badge.scss => badge.module.css} (70%) diff --git a/src/renderer/components/badge/badge.scss b/src/renderer/components/badge/badge.module.css similarity index 70% rename from src/renderer/components/badge/badge.scss rename to src/renderer/components/badge/badge.module.css index 2788ee8c1a..19dc8c7631 100644 --- a/src/renderer/components/badge/badge.scss +++ b/src/renderer/components/badge/badge.module.css @@ -19,39 +19,41 @@ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -.Badge { +.badge { position: relative; display: inline-block; - background: $colorVague; - color: $textColorSecondary; - border-radius: $radius; - padding: .2em .4em; - white-space: nowrap; max-width: 100%; - - &.interactive:hover { - background-color: $mainBackground; - cursor: pointer; - } - - &:not(.isExpanded) { - white-space: nowrap; - overflow: hidden; - text-overflow: ellipsis; - } - - &:not(.flat) { - background: $colorVague; - color: $textColorSecondary; - border-radius: $radius; - padding: .2em .4em; - } - - &.small { - font-size: $font-size-small; - } - - &.clickable { - cursor: pointer; - } +} + +.badge + .badge { + margin-left: 8px; +} + +.badge.interactive:hover { + background-color: var(--mainBackground); + cursor: pointer; +} + +.badge:not(.isExpanded) { + white-space: nowrap; + overflow: hidden; + text-overflow: ellipsis; +} + +.badge:not(.flat) { + background: var(--colorVague); + border-radius: 3px; + padding: .2em .4em; +} + +.small { + font-size: var(--font-size-small); +} + +.clickable { + cursor: pointer; +} + +.disabled { + opacity: 0.5; } diff --git a/src/renderer/components/badge/badge.tsx b/src/renderer/components/badge/badge.tsx index 71bf0013bb..3848d23481 100644 --- a/src/renderer/components/badge/badge.tsx +++ b/src/renderer/components/badge/badge.tsx @@ -19,10 +19,10 @@ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -import "./badge.scss"; +import styles from "./badge.module.css"; import React from "react"; -import { computed, observable } from "mobx"; +import { computed, makeObservable, observable } from "mobx"; import { observer } from "mobx-react"; import { cssNames } from "../../utils/cssNames"; import { TooltipDecoratorProps, withTooltip } from "../tooltip"; @@ -32,7 +32,8 @@ export interface BadgeProps extends React.HTMLAttributes, TooltipDecoratorP small?: boolean; flat?: boolean; label?: React.ReactNode; - isExpanded?: boolean; // always force state to this value + expandable?: boolean; + disabled?: boolean; } const badgeMeta = observable({ @@ -47,10 +48,21 @@ document.addEventListener("selectionchange", () => { @withTooltip @observer export class Badge extends React.Component { + static defaultProps: Partial = { + expandable: true + }; + @observable.ref elem: HTMLElement; @observable isExpanded = false; + constructor(props: BadgeProps) { + super(props); + makeObservable(this); + } + @computed get isExpandable() { + if (!this.props.expandable) return false; + return this.elem?.clientWidth < this.elem?.scrollWidth; } @@ -66,14 +78,15 @@ export class Badge extends React.Component { } render() { - const { className, label, small, children, isExpanded, flat, ...elemProps } = this.props; - const clickable = Boolean(this.props.onClick); - const classNames = cssNames("Badge", className, { - small, - flat, - clickable, - interactive: this.isExpandable, - isExpanded: isExpanded ?? this.isExpanded, + const { className, label, disabled, small, children, flat, expandable, ...elemProps } = this.props; + const clickable = Boolean(this.props.onClick) || this.isExpandable; + const classNames = cssNames(styles.badge, className, { + [styles.small]: small, + [styles.flat]: flat, + [styles.clickable]: clickable, + [styles.interactive]: this.isExpandable, + [styles.isExpanded]: this.isExpanded, + [styles.disabled]: disabled }); return (