Skip to the content

Hata旗

Load a native country or region flag, choose supported framing and keep each drawing’s source, author and licence.

Hata (旗) is Japanese for a flag. About the name

Contents

How-to

The element, the iframe and the builder

Every attribute of the hata-flag element, the iframe for a page with no scripts, framework use, and the builder in the demo.

One script tag and an element

One script tag and an element, for any page:

<script type="module" src="https://cdn.jsdelivr.net/npm/@johnmorrisdotca/hata@1/dist/element-define.js"></script>
<p>Where I live: <hata-flag code="CA-BC"></hata-flag> British Columbia</p>
<hata-flag code="JP-13" shape="round" size="48" border shadow></hata-flag>

The attributes

AttributeWhat it does
codeThe flag's code, in any case: JP, jp-13, CA-ON, DE-BY
shapeown (the default: its own proportions), 4:3, 1:1 or round, framed as flag(code, { shape }) frames it
sizeIts height in CSS pixels; left out, 1em, the height of the text around it, and any CSS height works
variantWhich of the place's flags in real use to draw, by its id from variantsOf(code): de-facto, stripes, local; left out, the default
dateThe day whose flag to draw, "1962-09-29": the flag then (variantOn), the legal one by default; a variant wins
whichWith date: legal (the default) or in-use, the flag flown in practice (not role, which is the ARIA role the element sets)
allow-restrictedDraw a flag the law restricts (Germany's of 1935 to 1945), for history and education; without it nothing is drawn, with data-restricted and the reason in data-reason
fitauto (the default), whole (all of the flag), crop (a crop at the side chosen for the flag), cover, hoist or contain, as frame() takes them
labelWhat a screen reader says; left out, the place's name
langen or ja, the language of that name; left out, the language of the page where the element is
themeauto (the default), light or dark: the colour of the border on a light or a dark page
border, shadowA hairline round the flag (a white flag on a white page needs one), and a soft shadow
loadinglazy (the default: loaded as it comes near the screen) or eager

It is drawn in the page's own document, with no shadow root: role="img" and an aria-label, and an <img> of the flag inside, so it is crisp at any size and the page's CSS sizes it like any inline element (--hata-flag-border-light, --hata-flag-border-dark and --hata-flag-shadow change the border and shadow). Every attribute is a property too, as React 19, Vue and Svelte set them, and await element.ready waits for the picture. It fires hata-load when drawn and hata-error when there is no flag for the code; then it draws nothing, takes no room, and says why once on the console. These are the family's conventions, from Kyuubu's <kyuubu-cube>.

A page that allows no scripts

For a site that allows no scripts, an iframe of the demo's embed page, which takes the same attributes as its query:

<iframe src="https://johnmorrisdotca.github.io/hata/embed.html?code=JP-13&amp;shape=round&amp;size=48" title="Flag of Tokyo" width="56" height="56" style="border:0" loading="lazy"></iframe>

In a framework

In a framework, import the definition once and use the tag:

import "@johnmorrisdotca/hata/element/define";
// React 19: <hata-flag code="FR-20R" size={32} />
// Vue: <hata-flag code="FR-20R" size="32" /> (with isCustomElement for "hata-")
// Svelte: <hata-flag code="FR-20R" size="32" />; Angular: CUSTOM_ELEMENTS_SCHEMA

The builder

The demo's detail panel has an Embed this flag builder: choose the shape, the height, the language, the border, the shadow and the theme, see it live, and copy the exact code as a custom element, a plain <img> of the CDN's file (framed by CSS as the package frames it), a data: URI, an iframe, an ES module, React, Vue, Svelte or Angular. The builder is one self-contained module, demo/embed-builder.js, that names no product: Hata's options and the code only Hata writes are in demo/embed-hata.js.

See also the flag element and events and embedding flags.

View sourceEdit on GitHubMade from the source of @johnmorrisdotca/hata 1.31.0.