UNPKG

@trimble-oss/moduswebcomponents

Version:

Modus Web Components is a modern, accessible UI library built with Stencil JS that provides reusable web components following Trimble's Modus design system. This updated version focuses on improved flexibility, enhanced theming options, comprehensive cust

191 lines (189 loc) 7.39 kB
import { h, Host } from "@stencil/core"; import { convertPropsToClasses } from "./modus-wc-typography.tailwind"; import { handleShadowDOMStyles } from "../base-component"; import { inheritAriaAttributes } from "../utils"; /** * A customizable typography component used to render text with different sizes, hierarchy, and weights. * * Note: * - When using heading elements (h1-h6), the default heading CSS styling can be accessed without modifying * the default size (size="md") and weight (weight="normal") properties. Default styling can be overridden by * providing your own custom values for the size or weight properties from the available options. * * - If both slot content and `label` are provided, only the slot content will be rendered * - Use the `label` prop when you need to dynamically update the text. */ export class ModusWCTypography { constructor() { this.inheritedAttributes = {}; /** Custom CSS class to apply to the typography element. */ this.customClass = ''; /** The hierarchy of the typography component. */ this.hierarchy = 'p'; /** The size of the font. */ this.size = 'md'; /** The weight of the text. */ this.weight = 'normal'; } componentWillLoad() { handleShadowDOMStyles(this.el); this.inheritedAttributes = inheritAriaAttributes(this.el); } getClasses() { const classList = ['modus-wc-typography']; // Check if we're dealing with a heading and have size/weight overrides const isHeading = ['h1', 'h2', 'h3', 'h4', 'h5', 'h6'].includes(this.hierarchy); const hasOverrides = this.size !== 'md' || this.weight !== 'normal'; if (isHeading && hasOverrides) { // Add a class to indicate overrides for headings classList.push('modus-wc-typography-override'); } const propClasses = convertPropsToClasses({ size: this.size, weight: this.weight, }); // The order CSS classes are added matters to CSS specificity if (propClasses) classList.push(propClasses); if (this.customClass) classList.push(this.customClass); return classList.join(' '); } render() { const Element = this.hierarchy; return (h(Host, { key: '41cf9b034e8dcdc27ef648a7e80b2b1a5ff02ff9' }, h(Element, Object.assign({ key: 'e173e76bb3e0de369e80e4edf4a533f6ed734f2e', class: this.getClasses() }, this.inheritedAttributes), h("slot", { key: 'e6e545ece442111e0d1eb7072e63e16f6fdf5ed4' }, this.label)))); } static get is() { return "modus-wc-typography"; } static get originalStyleUrls() { return { "$": ["modus-wc-typography.scss"] }; } static get styleUrls() { return { "$": ["modus-wc-typography.css"] }; } static get properties() { return { "customClass": { "type": "string", "attribute": "custom-class", "mutable": false, "complexType": { "original": "string", "resolved": "string | undefined", "references": {} }, "required": false, "optional": true, "docs": { "tags": [], "text": "Custom CSS class to apply to the typography element." }, "getter": false, "setter": false, "reflect": false, "defaultValue": "''" }, "hierarchy": { "type": "string", "attribute": "hierarchy", "mutable": false, "complexType": { "original": "TypographyHierarchy", "resolved": "\"h1\" | \"h2\" | \"h3\" | \"h4\" | \"h5\" | \"h6\" | \"p\"", "references": { "TypographyHierarchy": { "location": "import", "path": "../types", "id": "src/components/types.ts::TypographyHierarchy" } } }, "required": false, "optional": false, "docs": { "tags": [], "text": "The hierarchy of the typography component." }, "getter": false, "setter": false, "reflect": false, "defaultValue": "'p'" }, "label": { "type": "string", "attribute": "label", "mutable": false, "complexType": { "original": "string", "resolved": "string", "references": {} }, "required": true, "optional": false, "docs": { "tags": [], "text": "The text label to display." }, "getter": false, "setter": false, "reflect": false }, "size": { "type": "string", "attribute": "size", "mutable": false, "complexType": { "original": "TypographySize", "resolved": "\"2xl\" | \"3xl\" | \"4xl\" | \"5xl\" | \"6xl\" | \"7xl\" | \"8xl\" | \"9xl\" | \"lg\" | \"md\" | \"sm\" | \"xl\" | \"xs\" | undefined", "references": { "TypographySize": { "location": "import", "path": "../types", "id": "src/components/types.ts::TypographySize" } } }, "required": false, "optional": true, "docs": { "tags": [], "text": "The size of the font." }, "getter": false, "setter": false, "reflect": false, "defaultValue": "'md'" }, "weight": { "type": "string", "attribute": "weight", "mutable": false, "complexType": { "original": "TypographyWeight", "resolved": "\"bold\" | \"light\" | \"normal\" | \"semibold\" | undefined", "references": { "TypographyWeight": { "location": "import", "path": "../types", "id": "src/components/types.ts::TypographyWeight" } } }, "required": false, "optional": true, "docs": { "tags": [], "text": "The weight of the text." }, "getter": false, "setter": false, "reflect": false, "defaultValue": "'normal'" } }; } static get elementRef() { return "el"; } }