Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(Divider): improve code readability and performance #1159

Merged
merged 4 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Args, Meta, StoryObj } from '@storybook/web-components';
import { html } from 'lit-html';
import { html, nothing } from 'lit-html';
import { ifDefined } from 'lit-html/directives/if-defined.js';

import mdx from './bq-divider.mdx';
import { DIVIDER_ORIENTATION, DIVIDER_STROKE_LINECAP, DIVIDER_TITLE_ALIGNMENT } from '../bq-divider.types';
Expand Down Expand Up @@ -43,25 +44,19 @@ export default meta;
type Story = StoryObj;

const Template = (args: Args) => html`
<style>
.container {
/* width: 40%; */
height: ${args.orientation === 'vertical' ? html`70vh` : 'auto'};
}
</style>
<div class="container">
<div class="is-[70dvw]">
<bq-divider
orientation=${args.orientation}
orientation=${ifDefined(args.orientation)}
?dashed=${args.dashed}
stroke-color=${args['stroke-color']}
stroke-dash-width=${args['stroke-dash-width']}
stroke-dash-gap=${args['stroke-dash-gap']}
stroke-thickness=${args['stroke-thickness']}
stroke-basis=${args['stroke-basis']}
stroke-linecap=${args['stroke-linecap']}
title-alignment=${args['title-alignment']}
stroke-color=${ifDefined(args['stroke-color'])}
stroke-dash-width=${ifDefined(args['stroke-dash-width'])}
stroke-dash-gap=${ifDefined(args['stroke-dash-gap'])}
stroke-thickness=${ifDefined(args['stroke-thickness'])}
stroke-basis=${ifDefined(args['stroke-basis'])}
stroke-linecap=${ifDefined(args['stroke-linecap'])}
title-alignment=${ifDefined(args['title-alignment'])}
>
${args['title-text'] ? html`<p style="margin: 0; white-space: nowrap;">${args['title-text']}</p>` : null}
${args['title-text'] ? html`<p class="m-0 text-nowrap p-0">${args['title-text']}</p>` : nothing}
</bq-divider>
</div>
`;
Expand Down
57 changes: 23 additions & 34 deletions packages/beeq/src/components/divider/bq-divider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,6 @@ import {
} from './bq-divider.types';
import { getColorCSSVariable, getTextContent, hasSlotContent, validatePropValue } from '../../shared/utils';

const strokeDrawPositions = {
HORIZONTAL: (drawOffset: number) => {
return { x1: drawOffset, x2: '100%', y1: drawOffset, y2: drawOffset };
},
VERTICAL: (drawOffset: number) => {
return { x1: drawOffset, x2: drawOffset, y1: drawOffset, y2: '100%' };
},
};

/**
* @part base - The component's internal wrapper.
* @part dash-start - The component's internal svg wrapper for the start line of the divider's stroke
Expand Down Expand Up @@ -99,7 +90,7 @@ export class BqDivider {
// Ordered by their natural call order
// =====================================

componentWillLoad() {
connectedCallback() {
this.checkPropValues();
}

Expand All @@ -122,33 +113,33 @@ export class BqDivider {
// These methods cannot be called from the host element.
// =======================================================

private getStrokeDrawPositions = () => {
switch (this.orientation) {
case DIVIDER_ORIENTATION_ENUM.HORIZONTAL:
return strokeDrawPositions.HORIZONTAL(this.strokeThickness / 2);
case DIVIDER_ORIENTATION_ENUM.VERTICAL:
return strokeDrawPositions.VERTICAL(this.strokeThickness / 2);
default:
break;
}
};

private getStrokeDasharray = () => {
return `${this.strokeDashWidth}, ${this.strokeDashGap}`;
private handleSlotChange = () => {
this.hasTitle = hasSlotContent(this.titleElem) || !!getTextContent(this.titleElem.querySelector('slot'));
};

private getStrokeAttributes = () => {
private get strokeAttributes() {
return {
...this.getStrokeDrawPositions(),
...(this.dashed && { 'stroke-dasharray': this.getStrokeDasharray() }),
...this.strokeDrawPositions,
...(this.dashed && { 'stroke-dasharray': this.strokeDasharray }),
'stroke-linecap': this.strokeLinecap,
'stroke-width': this.strokeThickness,
};
};
}

private handleSlotChange = () => {
this.hasTitle = hasSlotContent(this.titleElem) || !!getTextContent(this.titleElem.querySelector('slot'));
};
private get strokeDrawPositions() {
const drawOffset = this.strokeThickness / 2;
const strokeDrawPositions = {
[DIVIDER_ORIENTATION_ENUM.HORIZONTAL]: { x1: drawOffset, x2: '100%', y1: drawOffset, y2: drawOffset },
[DIVIDER_ORIENTATION_ENUM.VERTICAL]: { x1: drawOffset, x2: drawOffset, y1: drawOffset, y2: '100%' },
};
const orientationMap = new Map(Object.entries(strokeDrawPositions));

return orientationMap.get(this.orientation);
}

private get strokeDasharray() {
return `${this.strokeDashWidth}, ${this.strokeDashGap}`;
}

// render() function
// Always the last one in the class.
Expand All @@ -161,8 +152,6 @@ export class BqDivider {
...(this.strokeBasis && { '--bq-divider--stroke-basis': `${this.strokeBasis}px` }),
};

const strokeAttributes = this.getStrokeAttributes();

return (
<Host style={styles}>
<div
Expand All @@ -178,11 +167,11 @@ export class BqDivider {
aria-orientation={this.orientation}
>
<svg class="bq-divider--stroke start" part="dash-start">
<line {...strokeAttributes} part="dash-start-line" />
<line {...this.strokeAttributes} part="dash-start-line" />
</svg>
<slot onSlotchange={this.handleSlotChange} />
<svg class={{ 'bq-divider--stroke end': true, '!hidden': !this.hasTitle }} part="dash-end">
<line {...strokeAttributes} part="dash-end-line" />
<line {...this.strokeAttributes} part="dash-end-line" />
</svg>
</div>
</Host>
Expand Down
6 changes: 3 additions & 3 deletions packages/beeq/src/components/divider/scss/bq-divider.scss
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
}

.bq-divider--stroke {
@apply h-[var(--bq-divider--stroke-thickness)] w-full flex-grow stroke-[color:var(--bq-divider--stroke-color)];
@apply flex-grow stroke-[color:var(--bq-divider--stroke-color)] bs-[var(--bq-divider--stroke-thickness)] is-full;

&.end {
@apply rotate-180;
Expand All @@ -22,9 +22,9 @@
}

.bq-divider--vertical {
@apply h-full flex-col items-center gap-[var(--bq-divider--title-marginX)];
@apply flex-col items-center gap-[var(--bq-divider--title-marginX)] is-full;

.bq-divider--stroke {
@apply h-full w-[var(--bq-divider--stroke-thickness)];
@apply bs-full is-[var(--bq-divider--stroke-thickness)];
}
}