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

View File

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