Skip to content

pennants/element ​

Web Component. import { definePennantElement } from 'pennants/element', or import 'pennants/element/register' for <pennant-flag>.

Classes ​

PennantElement ​

<pennant-flag code="ar" size="32" label="Argentina"> — lazily loads the flag into its shadow root. Fires pennant-load / pennant-error. With a base attribute (or definePennantElement(tag, { base })) the flag is fetched from <base>/svg/<shape>/<code>.svg instead of the bundled chunks.

Extends ​

  • Base

Constructors ​

Constructor ​
ts
new PennantElement(): PennantElement;

Prefer document.createElement('pennant-flag') or the tag in HTML after definePennantElement().

Returns ​

PennantElement

Overrides ​

Base.constructor

Properties ​

PropertyModifierTypeDefault valueDescription
defaultBasestaticstring | undefinedundefinedFetch base used when the element has no base attribute (set by definePennantElement).
observedAttributesstatic readonlyreadonly string[]PROPSAttributes the element reacts to: code, size, label, base, shape.

Accessors ​

base ​
Get Signature ​
ts
get base(): string | null;

Fetch-mode base URL (see pennantUrl). Reflects the base attribute.

Returns ​

string | null

Set Signature ​
ts
set base(value): void;

Sets the base attribute (fetch mode); null returns to the tag default.

Parameters ​
ParameterType
valuestring | null
Returns ​

void

code ​
Get Signature ​
ts
get code(): string | null;

Flag code (any casing). Reflects the code attribute.

Returns ​

string | null

Set Signature ​
ts
set code(value): void;

Sets (or with null removes) the code attribute.

Parameters ​
ParameterType
valuestring | null
Returns ​

void

label ​
Get Signature ​
ts
get label(): string | null;

Accessible name; defaults to the English flag name, "" = decorative.

Returns ​

string | null

Set Signature ​
ts
set label(value): void;

Sets the label attribute; "" marks it decorative, null restores the default name.

Parameters ​
ParameterType
valuestring | null
Returns ​

void

shape ​
Get Signature ​
ts
get shape(): "pennant" | "rect" | "square" | "circle" | null;

Flag shape: pennant (default), rect, square or circle. Reflects the shape attribute.

Returns ​

"pennant" | "rect" | "square" | "circle" | null

Set Signature ​
ts
set shape(value): void;

Sets the shape attribute; null returns to pennant. Unknown shapes fire pennant-error.

Parameters ​
ParameterType
value"pennant" | "rect" | "square" | "circle" | null
Returns ​

void

size ​
Get Signature ​
ts
get size(): string | null;

Height: a number of px or any CSS length. Reflects the size attribute.

Returns ​

string | null

Set Signature ​
ts
set size(value): void;

Sets the size attribute; numbers are pixels. null removes it.

Parameters ​
ParameterType
valuestring | number | null
Returns ​

void

Interfaces ​

DefinePennantElementOptions ​

Options of definePennantElement.

Properties ​

PropertyTypeDescription
base?stringFetch mode for every instance of this tag: load <base>/svg/<shape>/<code>.svg with fetchPennant instead of the bundled flag chunks. A per-element base attribute takes precedence.

PennantErrorDetail ​

detail of the pennant-error event (unknown code, failed download or invalid fetched markup).

Properties ​

PropertyTypeDescription
codestringThe code attribute value that failed.
errorunknownThe rejection reason: a RangeError for unknown codes, otherwise the load/fetch error.

PennantLoadDetail ​

detail of the pennant-load event, fired once the flag is rendered.

Properties ​

PropertyTypeDescription
codePennantCodeNormalised code of the flag that was loaded.

Variables ​

PENNANT_ELEMENT_CSS ​

ts
const PENNANT_ELEMENT_CSS: string;

Shadow styles. Includes the bar theming rule: document styles don't reach the shadow root, but custom properties inherit, so --pennant-bar-light / --pennant-bar-dark on any ancestor recolour the bar.

Functions ​

definePennantElement() ​

ts
function definePennantElement(tagName?, options?): typeof PennantElement | undefined;

Registers the element (default tag pennant-flag). Idempotent: returns the already-registered constructor if the tag exists (options are then ignored). No-op (returns undefined) where customElements is unavailable, e.g. during SSR. { base } switches the tag to fetch mode.

Parameters ​

ParameterTypeDefault value
tagName?string'pennant-flag'
options?DefinePennantElementOptions{}

Returns ​

typeof PennantElement | undefined

MIT licensed. Flag artwork from flag-icons (MIT).