1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

fix: hover only intercative badges (with extra content)

Signed-off-by: Roman <ixrock@gmail.com>
This commit is contained in:
Roman 2020-11-17 11:58:39 +02:00
parent 2951a7b1af
commit ae68ea2d51
2 changed files with 31 additions and 30 deletions

View File

@ -1,30 +1,24 @@
.Badge { .Badge {
position: relative;
display: inline-block; display: inline-block;
background: $colorVague; background: $colorVague;
color: $textColorSecondary; color: $textColorSecondary;
border-radius: $radius; border-radius: $radius;
padding: .2em .4em; padding: .2em .4em;
white-space: nowrap;
max-width: 100%; max-width: 100%;
position: relative;
cursor: pointer;
&:hover { &.interactive:hover {
background-color: $mainBackground; background-color: $mainBackground;
cursor: pointer;
}
&:not(.isExpanded) {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
} }
&.small { &.small {
font-size: $font-size-small; font-size: $font-size-small;
} }
.label-content {
overflow: hidden;
text-overflow: ellipsis;
&.isExpanded {
overflow: unset;
text-overflow: unset;
white-space: normal;
}
}
} }

View File

@ -1,44 +1,51 @@
import "./badge.scss" import "./badge.scss"
import React from "react"; import React from "react";
import { computed, observable } from "mobx";
import { observer } from "mobx-react";
import { cssNames } from "../../utils/cssNames"; import { cssNames } from "../../utils/cssNames";
import { TooltipDecoratorProps, withTooltip } from "../tooltip"; import { TooltipDecoratorProps, withTooltip } from "../tooltip";
import { observer } from "mobx-react";
import { autobind } from "../../utils"; import { autobind } from "../../utils";
import { observable } from "mobx";
export interface BadgeProps extends React.HTMLAttributes<any>, TooltipDecoratorProps { export interface BadgeProps extends React.HTMLAttributes<any>, TooltipDecoratorProps {
small?: boolean; small?: boolean;
label?: React.ReactNode; label?: React.ReactNode;
isExpanded?: boolean; // by default: false (minimized) isExpanded?: boolean; // always force state to this value
} }
@withTooltip @withTooltip
@observer @observer
export class Badge extends React.Component<BadgeProps> { export class Badge extends React.Component<BadgeProps> {
@observable.ref elem: HTMLElement;
@observable isExpanded = false; @observable isExpanded = false;
@computed get isExpandable() {
return this.elem?.clientWidth < this.elem?.scrollWidth;
}
@autobind() @autobind()
onMouseRelease() { onMouseUp(evt: React.MouseEvent) {
const textSelected = document.getSelection().toString().trim() !== ""; const isTextSelected = !!document.getSelection().toString().trim();
if (textSelected) return; if (!this.isExpandable || isTextSelected) return; // no action required
this.isExpanded = this.props.isExpanded /*force to use state from outside*/ ?? !this.isExpanded /*toggle*/; this.isExpanded = !this.isExpanded;
}
@autobind()
bindRef(elem: HTMLElement) {
this.elem = elem;
} }
render() { render() {
const { className, label, small, children, isExpanded, ...elemProps } = this.props; const { className, label, small, children, isExpanded, ...elemProps } = this.props;
const classNames = cssNames("Badge", className, { const classNames = cssNames("Badge", className, {
small: small, small: small,
}) interactive: this.isExpandable,
const labelClass = cssNames("label-content", { isExpanded: isExpanded ?? this.isExpanded,
isExpanded: this.isExpanded,
}) })
return ( return (
<div {...elemProps} className={classNames}> <div {...elemProps} className={classNames} onMouseUp={this.onMouseUp} ref={this.bindRef}>
<div className={labelClass} onMouseUp={this.onMouseRelease}> {label}
{label} {children}
{children}
</div>
</div> </div>
) )
} }