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 {
position: relative;
display: inline-block;
background: $colorVague;
color: $textColorSecondary;
border-radius: $radius;
padding: .2em .4em;
white-space: nowrap;
max-width: 100%;
position: relative;
cursor: pointer;
&:hover {
&.interactive:hover {
background-color: $mainBackground;
cursor: pointer;
}
&:not(.isExpanded) {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
&.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 React from "react";
import { computed, observable } from "mobx";
import { observer } from "mobx-react";
import { cssNames } from "../../utils/cssNames";
import { TooltipDecoratorProps, withTooltip } from "../tooltip";
import { observer } from "mobx-react";
import { autobind } from "../../utils";
import { observable } from "mobx";
export interface BadgeProps extends React.HTMLAttributes<any>, TooltipDecoratorProps {
small?: boolean;
label?: React.ReactNode;
isExpanded?: boolean; // by default: false (minimized)
isExpanded?: boolean; // always force state to this value
}
@withTooltip
@observer
export class Badge extends React.Component<BadgeProps> {
@observable.ref elem: HTMLElement;
@observable isExpanded = false;
@computed get isExpandable() {
return this.elem?.clientWidth < this.elem?.scrollWidth;
}
@autobind()
onMouseRelease() {
const textSelected = document.getSelection().toString().trim() !== "";
if (textSelected) return;
this.isExpanded = this.props.isExpanded /*force to use state from outside*/ ?? !this.isExpanded /*toggle*/;
onMouseUp(evt: React.MouseEvent) {
const isTextSelected = !!document.getSelection().toString().trim();
if (!this.isExpandable || isTextSelected) return; // no action required
this.isExpanded = !this.isExpanded;
}
@autobind()
bindRef(elem: HTMLElement) {
this.elem = elem;
}
render() {
const { className, label, small, children, isExpanded, ...elemProps } = this.props;
const classNames = cssNames("Badge", className, {
small: small,
})
const labelClass = cssNames("label-content", {
isExpanded: this.isExpanded,
interactive: this.isExpandable,
isExpanded: isExpanded ?? this.isExpanded,
})
return (
<div {...elemProps} className={classNames}>
<div className={labelClass} onMouseUp={this.onMouseRelease}>
{label}
{children}
</div>
<div {...elemProps} className={classNames} onMouseUp={this.onMouseUp} ref={this.bindRef}>
{label}
{children}
</div>
)
}