Skip to content

Commit

Permalink
Merge pull request #2762 from db-ui/2698-improve-performance-of-has-c…
Browse files Browse the repository at this point in the history
…ss-selector

refactor: added a relevant selector part
  • Loading branch information
mfranzke authored Jun 24, 2024
2 parents d71e4e3 + 5b211f4 commit ed4b798
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 40 deletions.
41 changes: 38 additions & 3 deletions packages/components/src/components/badge/badge.lite.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,45 @@
import { useMetadata, useRef, useStore } from '@builder.io/mitosis';
import {
onMount,
onUpdate,
Show,
useMetadata,
useRef,
useStore
} from '@builder.io/mitosis';
import { DBBadgeProps, DBBadgeState } from './model';
import { cls } from '../../utils';
import { DEFAULT_LABEL } from '../../shared/constants';

useMetadata({
isAttachedToShadowDom: true
});

export default function DBBadge(props: DBBadgeProps) {
const ref = useRef<HTMLSpanElement>(null);
const state = useStore<DBBadgeState>({});
const state = useStore<DBBadgeState>({
initialized: false
});

onMount(() => {
state.initialized = true;
});

onUpdate(() => {
if (ref && state.initialized) {
if (props.placement?.startsWith('corner')) {
let parent = ref.parentElement;

if (parent && parent.localName.includes('badge')) {
// Angular workaround
parent = parent.parentElement;
}

if (parent) {
parent.setAttribute('data-has-badge', 'true');
}
}
}
}, [ref, state.initialized]);

return (
<span
Expand All @@ -18,7 +49,11 @@ export default function DBBadge(props: DBBadgeProps) {
data-semantic={props.semantic}
data-size={props.size}
data-emphasis={props.emphasis}
data-placement={props.placement}>
data-placement={props.placement}
data-label={
props.placement?.startsWith('corner') &&
(props.label ?? DEFAULT_LABEL)
}>
{props.children}
</span>
);
Expand Down
31 changes: 17 additions & 14 deletions packages/components/src/components/badge/badge.scss
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,11 @@
@extend %default-button;

@include tag-components.get-tag-colors();
border-radius: 42px; // extreme radius
border-radius: variables.$db-border-radius-full;
padding-inline: variables.$db-spacing-fixed-2xs;
block-size: fit-content;
inline-size: fit-content;

&:empty,
> span:empty {
block-size: var(--badge-size);
inline-size: var(--badge-size);
padding: 0;
}

&:has(.db-icon) {
padding: variables.$db-spacing-fixed-3xs;
}
Expand All @@ -69,11 +62,6 @@
@extend %db-overwrite-font-size-sm;
padding-inline: variables.$db-spacing-fixed-xs;

&:empty,
> span:empty {
padding: 0;
}

&:has(.db-icon) {
padding: variables.$db-spacing-fixed-2xs;
}
Expand All @@ -84,13 +72,28 @@
}
}

&:empty,
> span:empty {
block-size: var(--badge-size);
inline-size: var(--badge-size);
padding: 0;
}

&[data-placement^="corner"] {
@extend %absolute-badge;

&[data-label] {
&::before {
content: attr(data-label);
position: fixed;
font-size: 0;
}
}
}
}

// Global styles

:has(> .db-badge[data-placement^="corner"]) {
[data-has-badge="true"] {
position: relative;
}
10 changes: 8 additions & 2 deletions packages/components/src/components/badge/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import {
EmphasisProps,
GlobalProps,
GlobalState,
SizeProps
SizeProps,
InitializedState
} from '../../shared/model';

export const BadgePlacementList = [
Expand All @@ -22,6 +23,11 @@ export interface DBBadgeDefaultProps {
* The `placement` attributes `corner-*` values change the position to absolute and adds a transform based on the placement.
*/
placement?: BadgePlacementType;

/**
* Describes the badge for a11y if you use placement attribute with `corner-*`
*/
label?: string;
}

export type DBBadgeProps = DBBadgeDefaultProps &
Expand All @@ -32,4 +38,4 @@ export type DBBadgeProps = DBBadgeDefaultProps &

export interface DBBadgeDefaultState {}

export type DBBadgeState = DBBadgeDefaultState & GlobalState;
export type DBBadgeState = DBBadgeDefaultState & GlobalState & InitializedState;
17 changes: 14 additions & 3 deletions packages/components/src/components/tooltip/tooltip.lite.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import {
useStore
} from '@builder.io/mitosis';
import { DBTooltipProps, DBTooltipState } from './model';
import { cls, handleDataOutside } from '../../utils';
import { cls, handleDataOutside, uuid } from '../../utils';
import { ClickEvent } from '../../shared/model';
import { DEFAULT_ID } from '../../shared/constants';

useMetadata({
isAttachedToShadowDom: true
Expand All @@ -17,6 +18,7 @@ export default function DBTooltip(props: DBTooltipProps) {
const ref = useRef<HTMLDivElement>(null);
// jscpd:ignore-start
const state = useStore<DBTooltipState>({
_id: DEFAULT_ID,
initialized: false,
handleClick: (event: ClickEvent<HTMLElement>) => {
event.stopPropagation();
Expand All @@ -27,18 +29,27 @@ export default function DBTooltip(props: DBTooltipProps) {
});

onMount(() => {
state._id = props.id || 'tooltip-' + uuid();
state.initialized = true;
});

onUpdate(() => {
if (ref && state.initialized) {
const parent = ref.parentElement;
let parent = ref.parentElement;

if (parent && parent.localName.includes('tooltip')) {
// Angular workaround
parent = parent.parentElement;
}

if (parent) {
['mouseenter', 'focus'].forEach((event) => {
parent.addEventListener(event, () =>
state.handleAutoPlacement()
);
});
parent.setAttribute('data-has-tooltip', 'true');
parent.setAttribute('aria-describedby', state._id);
}

state.initialized = false;
Expand All @@ -53,7 +64,7 @@ export default function DBTooltip(props: DBTooltipProps) {
role="tooltip"
ref={ref}
className={cls('db-tooltip', props.className)}
id={props.id}
id={state._id}
data-emphasis={props.emphasis}
data-animation={props.animation}
data-delay={props.delay}
Expand Down
7 changes: 2 additions & 5 deletions packages/components/src/components/tooltip/tooltip.scss
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,7 @@ $tooltip-arrow-shadow-size: variables.$db-sizing-2xs;
// Global styles

@mixin show-popover($state) {
:has(
> :is(db-tooltip, dbtooltip) > .db-tooltip:not([data-open]),
> .db-tooltip:not([data-open])
) {
[data-has-tooltip="true"] {
&:#{$state} {
.db-tooltip:first-of-type {
@extend %show-popover;
Expand All @@ -124,7 +121,7 @@ $tooltip-arrow-shadow-size: variables.$db-sizing-2xs;
}
}

:has(> db-tooltip, > dbtooltip, > .db-tooltip) {
[data-has-tooltip="true"] {
position: relative;
}

Expand Down
7 changes: 3 additions & 4 deletions scripts/dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@ inquirer
return;
}

// Let startCommand = 'npm-run-all -p start:foundations dev:sass';
let startCommand = 'npm run start:foundations & npm run dev:sass';
let startCommand = 'npm-run-all -p start:foundations dev:sass';

const currentAnswers = answers?.frameworks;

Expand All @@ -80,12 +79,12 @@ inquirer
);

if (isAnswerSelected) {
startCommand += ` & npm run dev:${framework}-components`;
startCommand += ` dev:${framework}-components`;
}
}

for (const currentAnswer of currentAnswers) {
startCommand += ` & npm run start-showcase:${currentAnswer} --if-present`;
startCommand += ` start-showcase:${currentAnswer}`;
}

// TODO: Handle child process better
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
let-exampleIndex="exampleIndex"
let-variantIndex="variantIndex"
>
<db-button [describedbyid]="exampleProps.id">
<db-button>
{{ exampleName }}
<db-tooltip
[width]="exampleProps.width"
Expand All @@ -19,7 +19,6 @@
[animation]="exampleProps.animation"
[delay]="exampleProps.delay"
[variant]="exampleProps.variant"
[id]="exampleProps.id"
>{{ exampleProps.content }}</db-tooltip
>
</db-button>
Expand Down
8 changes: 3 additions & 5 deletions showcases/react-showcase/src/components/tooltip/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,17 @@ const getTooltip = ({
delay,
content,
animation,
variant,
id
variant
}: DBTooltipProps & { content: string }) => (
<DBButton describedbyid={id}>
<DBButton>
{children}
<DBTooltip
width={width}
emphasis={emphasis}
placement={placement}
animation={animation}
delay={delay}
variant={variant}
id={id}>
variant={variant}>
{content}
</DBTooltip>
</DBButton>
Expand Down
3 changes: 1 addition & 2 deletions showcases/vue-showcase/src/components/tooltip/Tooltip.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { DBButton, DBTooltip } from "../../../../../output/vue/src";
<template
#example="{ exampleIndex, variantIndex, exampleName, exampleProps }"
>
<DBButton :describedbyid="exampleProps.id">
<DBButton>
{{ exampleName }}
<DBTooltip
:width="exampleProps.width"
Expand All @@ -18,7 +18,6 @@ import { DBButton, DBTooltip } from "../../../../../output/vue/src";
:animation="exampleProps.animation"
:delay="exampleProps.delay"
:variant="exampleProps.variant"
:id="exampleProps.id"
>
{{ exampleProps.content }}
</DBTooltip>
Expand Down

0 comments on commit ed4b798

Please sign in to comment.