Patterns: Constants
Location, organization, naming, and extract-vs-inline rules for global constants. Load via the
patterns-componentscontext group.
Two-File Layout
There is no src/constants/ directory in hbf-webchat. Global constants live in two flat files:
| File | Purpose |
|---|---|
src/constants.ts | SVG strings and DOMPurify sanitize allowlists |
src/misc/constants.ts | Reused CSS class-name strings (with _CLASSNAME suffix) and additional inline SVGs |
No barrel index.ts. Import directly from the specific file.
What lives where
src/constants.ts:
DEFAULT_SEND_ICON— send-button SVG markupMUTED_MIC_SVG,MIC_SVG— microphone icon SVGsSANITIZE_HTML_OPTIONS— DOMPurify allowlist for general HTML rendering (allowed tags, attrs, URI regex)SVG_SANITIZE_OPTIONS— stricter DOMPurify allowlist for SVG inputs (usesFORBID_TAGS/FORBID_ATTR)
src/misc/constants.ts:
- 11
_CLASSNAMEconstants (CSS class-name strings used in multiple files) errorSvg— inline error SVG markupDIVIDER_SVG,END_LIVECHAT_SVG— additional inline SVGs
Naming Rules
- Exported constants:
UPPER_SNAKE_CASEalways. One legacy exception insrc/misc/constants.ts:13(errorSvg) — do not introduce new camelCase constants. _CLASSNAMEsuffix for CSS class-name strings that are shared across files. The value is the raw class string, not a className-builder helper.- Module-private constants (used only inside one file) live as a top-level
constin that file with normalcamelCaseorUPPER_SNAKE_CASEdepending on their nature (variable vs token).
_CLASSNAME examples
// src/misc/constants.ts
export const CUSTOM_BUTTONS_CLASSNAME = "custom-buttons__container";
export const MENU_BUTTON_CLASSNAME = "bubble-widget__menu__button";
export const END_LIVECHAT_BUTTON_CLASSNAME = "webchat__end-livechat__button";
export const END_LIVECHAT_CONFIRMATION_CONTAINER_CLASSNAME = "webchat__end-livechat__confirmation";
export const END_LIVECHAT_CONFIRMATION_CONTAINER_TITLE_CLASSNAME = `${END_LIVECHAT_CONFIRMATION_CONTAINER_CLASSNAME}__title`;
The full list (all 11) is in src/misc/constants.ts lines 1–11.
What to Extract vs Inline
Extract to src/constants.ts or src/misc/constants.ts:
- Inline SVG strings used in 2+ files
- DOMPurify allowlists (one source of truth for sanitization)
- CSS class-name strings referenced from both a
.tsxtemplate and a SCSS selector or another.tsfile (with_CLASSNAMEsuffix) - Any string or number that is duplicated across 2+ files
Keep inline (top of the file as const):
- One-off CSS class strings used in only one component
- Local maps used by a single
switchorif/elsechain - Helper constants that exist only for readability inside one function
Rule of thumb: if grep shows the same literal in 2+ files, hoist it to the appropriate constants file.
Enums and Domain Types
Enum-like values do not go in src/constants.ts. They belong in:
src/interfaces/enums.ts— TypeScriptenumdeclarations for closed sets shared across files (DIRECT_LINE,WEBCHAT,LiveChatEventType,SendBoxState,ChatMessageType,HeaderStyle,NotificationLevelsis inCustomNotificationInterfaces.ts).- Domain interface files (
src/interfaces/csat.ts,src/interfaces/ratingForm.ts) — string-literal union types used as a discriminator field on a single domain interface (type CSATStyle = "numbers" | "stars" | "emotions").
Use TS enum when the set is closed and reused across multiple files. Use string-literal unions when the set is style-discriminator-style and lives next to a single domain interface. See typescript.md for the full rule.
Why Two Files Instead of One
The split is historical: src/constants.ts predates src/misc/constants.ts. The newer file accreted around CSS class-name strings, which are referenced from the webchat__* BEM overrides in src/styles/*.scss. Keeping the two files separate avoids forcing every consumer of SANITIZE_HTML_OPTIONS to also pull in the class-name strings.
There is no plan to merge them. New constants pick the right file by content type:
- SVG markup or sanitize options →
src/constants.ts - Class-name strings (
_CLASSNAMEsuffix) →src/misc/constants.ts - Anything else (numeric limits, timing, feature flags) → start a new file under
src/misc/if there are 3+ such constants; otherwise inline in the consuming file