import { Component, Prop, Element, h, Watch, AttachInternals } from '@stencil/core'; import { badgeColourDefinitions, BadgeColour } from './ontario-badge.types'; import { ConsoleMessageClass } from '../../utils/console-message/console-message'; import { isClientSideRendering } from '../../utils/common/environment'; import { validateValueAgainstArray } from '../../utils/validation/validation-functions'; /** * Ontario Badge displays concise status labels and metadata. * * For component guidance, see: * - https://designsystem.ontario.ca/components/detail/badges.html * - https://designsystem.ontario.ca/developer-docs/components/ontario-badge/ */ @Component({ tag: 'ontario-badge', styleUrl: 'ontario-badge.scss', shadow: true, }) export class OntarioBadge { @Element() host: HTMLElement; @AttachInternals() internals: ElementInternals; /** * The colour of the badge. */ @Prop({ mutable: true }) colour: BadgeColour = 'teal'; /** * The label for the badge. * * Offical guidance is to keep the label length within 15 characters. */ @Prop() label: string; /** * An aria label for screen readers. * * Used to provide more context to screen readers if necessary. * * This property is optional. * * @example * In progress */ @Prop({ mutable: true }) ariaLabelText?: string; /** * Watch for changes in the `label` prop for validation purposes. * * If no `label` value or host.textContent is provided, a warning message will be printed. */ @Watch('label') validateLabel() { if (!this.getBadgeLabel()) { const message = new ConsoleMessageClass(); message .addDesignSystemTag() .addMonospaceText(' a label ') .addRegularText('for') .addMonospaceText(' ') .addRegularText('was not provided.') .printMessage(); } } /** * Watch for changes to the `colour` prop. * * If no `colour` is passed, or if the `colour` does not match the `colour` options, a default value of 'teal' will be applied. * * @returns {BadgeColour} */ @Watch('colour') validateColour(): BadgeColour { /** * Optional mapping to provide backward compatibility * for older camelCase colour values (e.g., `lightTeal`, `darkGrey`). * * These legacy values are still accepted but will be transformed into * the new kebab-case format and a console warning will be printed. * * This compatibility layer can be removed in the next major version bump. */ const legacyBadgeColourAliases: Record = { lightTeal: 'light-teal', darkGrey: 'dark-grey', }; // Check for legacy camelCase values if (this.colour in legacyBadgeColourAliases) { const mapped = legacyBadgeColourAliases[this.colour as keyof typeof legacyBadgeColourAliases]; const message = new ConsoleMessageClass(); message .addDesignSystemTag() .addMonospaceText(` colour ${this.colour} `) .addRegularText('for') .addMonospaceText(' ') .addRegularText('has been deprecated. Please use kebab-case values instead (e.g., "light-teal", "dark-grey").') .printMessage(); return this.setBadgeColour(mapped); } if (this.colour) { if (validateValueAgainstArray(this.colour, badgeColourDefinitions)) { return this.colour; } else { const message = new ConsoleMessageClass(); message .addDesignSystemTag() .addMonospaceText(` colour ${this.colour} `) .addRegularText('for') .addMonospaceText(' ') .addRegularText('does not match one of the valid colour types. A default colour of `teal` will be applied.') .printMessage(); } } return this.setBadgeColour('teal'); } /** * Programatically set and return the colour prop. * * @prop {BadgeColour} colour * * @returns {BadgeColour} */ setBadgeColour(colour: BadgeColour): BadgeColour { this.colour = colour; return this.colour; } /** * Returns badge label depending on if the label prop was set or * if the host.textContent (text in-between the opening and closing tag) was set. * * The label prop takes priority. * * @returns {string | null} */ getBadgeLabel(): string | null { if (this.label) return this.label; // SSR safety check: ensure we're in the client-side environment before accessing textContent if (isClientSideRendering() && typeof this.host?.textContent !== 'undefined') { return this.host.textContent; } return null; } componentWillLoad() { this.validateColour(); this.validateLabel(); } render() { return ( {this.getBadgeLabel()} ); } }