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

extended badge content by click, selecting text from label doesn't trigger auto-closing

Signed-off-by: Roman <ixrock@gmail.com>
This commit is contained in:
Roman 2020-11-16 20:02:21 +02:00
parent 3650dac09b
commit 2951a7b1af
2 changed files with 26 additions and 52 deletions

View File

@ -7,31 +7,24 @@
white-space: nowrap;
max-width: 100%;
position: relative;
cursor: pointer;
>.children {
overflow: hidden;
text-overflow: ellipsis;
&:hover {
background-color: $mainBackground;
}
&.small {
font-size: $font-size-small;
}
&.expanded {
overflow: unset;
text-overflow: unset;
white-space: unset;
}
.label-content {
overflow: hidden;
text-overflow: ellipsis;
> .expansionIcon {
transform: scaleX(-1);
position: absolute;
z-index: 1;
right: -6px;
bottom: -6px;
color: $textColorAccent;
background-color: $halfGray;
border-radius: 50%;
padding: 2px;
&.isExpanded {
overflow: unset;
text-overflow: unset;
white-space: normal;
}
}
}

View File

@ -6,55 +6,36 @@ import { TooltipDecoratorProps, withTooltip } from "../tooltip";
import { observer } from "mobx-react";
import { autobind } from "../../utils";
import { observable } from "mobx";
import { Icon } from "../icon";
export interface BadgeProps extends React.HTMLAttributes<any>, TooltipDecoratorProps {
small?: boolean;
label?: React.ReactNode;
isExpanded?: boolean; // by default: false (minimized)
}
@withTooltip
@observer
export class Badge extends React.Component<BadgeProps> {
private elem: HTMLElement;
@observable isExpanded = false
@observable canBeExpanded = false
componentDidMount() {
this.canBeExpanded = this.elem.offsetWidth < this.elem.scrollWidth
}
@observable isExpanded = false;
@autobind()
onClick() {
if (this.canBeExpanded) {
this.isExpanded = !this.isExpanded
}
}
renderExpansionIcon() {
if (!this.canBeExpanded) {
return null
}
const material = this.isExpanded ? "close_fullscreen" : "open_in_full"
return <Icon
className="expansionIcon"
size={12}
material={material}
focusable={false}
onClick={this.onClick}
/>
onMouseRelease() {
const textSelected = document.getSelection().toString().trim() !== "";
if (textSelected) return;
this.isExpanded = this.props.isExpanded /*force to use state from outside*/ ?? !this.isExpanded /*toggle*/;
}
render() {
const { className, label, small, children, ...elemProps } = this.props;
const classNames = cssNames("Badge", className, { small, expanded: this.isExpanded })
const { className, label, small, children, isExpanded, ...elemProps } = this.props;
const classNames = cssNames("Badge", className, {
small: small,
})
const labelClass = cssNames("label-content", {
isExpanded: this.isExpanded,
})
return (
<div className={classNames} {...elemProps}>
{this.renderExpansionIcon()}
<div className="children" ref={ref => (this.elem = ref)}>
<div {...elemProps} className={classNames}>
<div className={labelClass} onMouseUp={this.onMouseRelease}>
{label}
{children}
</div>