@vettvangur/hyphen
v1.2.0
Published
Vettvangur | Responsive language-aware hyphenation
Maintainers
Keywords
Readme
@vettvangur/hyphen
Responsive, language-aware hyphenation for headlines: long words are hyphenated only when they cannot fit on a line by themselves.
initHyphenation(vanilla JS) measures elements in the browser and inserts soft hyphens.Hyphenate(React,@vettvangur/hyphen/react) renders the hyphenation on the server and the client alike, and leaves the line breaking to the browser's layout.
Install
pnpm add @vettvangur/hyphen [email protected]Vanilla JS
import { initHyphenation } from '@vettvangur/hyphen'Whole page (default — targets elements with a headline class):
await initHyphenation()Custom classes:
await initHyphenation(document, { classes: ['my-heading', 'display-text'] })Each token matches exact class names and prefixes — headline matches both headline and headline-xl.
Scoped to a subtree:
await initHyphenation(someElement)React
Use the Hyphenate component from @vettvangur/hyphen/react instead of initHyphenation.
initHyphenation writes textContent straight into the DOM, which breaks elements React renders.
pnpm add @vettvangur/hyphen [email protected]react (18 or later) is an optional peer dependency — install it if the project does not already have it.
Usage
import { Hyphenate } from '@vettvangur/hyphen/react'
export default function Hero({ title }) {
return (
<h1 className="headline">
<Hyphenate lang="is">{title}</Hyphenate>
</h1>
)
}| Prop | |
|------|---|
| children | Plain text to hyphenate. |
| lang | is (or is-IS, …) for Icelandic; anything else is English. Defaults to en. Pass the page's locale. |
How it works
Hyphenate inserts soft hyphens into every word that has hyphenation points and wraps each of
those words in an inline-block <span>:
- A word that fits on a line moves to the next line whole, like any other word. Its soft hyphens are never used.
- A word wider than the line is capped at the line width (
max-width: 100%), and the browser breaks it at the soft hyphens.
The browser's own layout makes that decision, at any width, so nothing is measured and nothing runs after load. Resizing and web fonts finishing loading are handled by the browser.
Server and client
The output depends only on children and lang, so the server and the client render the same
markup:
- Works in React Server Components and in client components — no
'use client', hooks or effects. - The server HTML already contains the final hyphenation. Hydration matches and nothing changes on screen after the page loads.
The hyphenation patterns are imported statically, because rendering is synchronous. In a server component they stay on the server. In a client component they ship to the browser: about 16 kB gzipped for Icelandic and 22 kB for English (both are imported).
Next.js App Router
Render it straight from a server component:
import { Hyphenate } from '@vettvangur/hyphen/react'
export default async function Page({ params }) {
const { locale } = await params
const page = await getPage(locale)
return (
<h1 className="headline">
<Hyphenate lang={locale}>{page.title}</Hyphenate>
</h1>
)
}Layout differences from initHyphenation
A word that has to break takes the full line width, so the text before it ends its line and the
text after it starts on a new one. At a narrow width Ný Hæstaréttarlögmannsstofa er hér
renders as:
Ný
Hæstaréttarlög-
mannsstofa
er hérwhere initHyphenation would put mannsstofa er hér on one line. This only happens when a word
cannot fit on a line by itself.
Rich text
Hyphenate takes a plain string. For headings with markup, wrap each text run:
<h1 className="headline">
<Hyphenate lang="is">{lead}</Hyphenate> <em><Hyphenate lang="is">{emphasis}</Hyphenate></em>
</h1>Words without hyphenation points
A word the patterns cannot hyphenate is left as plain text and can overflow a narrow line. Give headlines a fallback:
.headline {
overflow-wrap: break-word;
}Do not mix with initHyphenation
initHyphenation() on document targets every headline element on the page, including ones
React renders. Use either Hyphenate or initHyphenation, not both on the same elements.
Language
Icelandic (is) and English are supported; anything that is not Icelandic is hyphenated as English.
initHyphenationdetects the language from the nearest[lang]ancestor attribute or<html lang>.Hyphenatetakes it as thelangprop, because the server has no DOM to read it from.
CSS classes
initHyphenation marks the elements it processes:
| Class | When added |
|-------|-----------|
| js-hyphen-manual | Soft hyphens (­) were inserted into a word |
| js-hyphen-emergency | A word couldn't be hyphenated — overflow fallback should be applied |
Example CSS:
.js-hyphen-emergency {
overflow-wrap: break-word;
}Auto re-runs
When initHyphenation is called with document as the root, it automatically re-runs on:
- Window resize (debounced 150 ms)
- Font load events (
document.fonts)
