0.7.9 • Published 16 days ago

mimetic v0.7.9

Weekly downloads
2
License
MIT
Repository
github
Last release
16 days ago

Mimetic

A tiny library for elegantly scaling fonts

"Mimetic" - relating to, constituting, or habitually practising mimesis

The Problem

You want your fonts to scale to the viewport because its not the 1970s. Although you can scale fonts using viewport-percentage lengths it's not usually recommended:

  • Fonts using viewport units will not be affected by the browser's built-in zoom, thus breaking accessibiilty
  • Responsive design becomes more tedious since each font will require an additional viewport value to be paired with a fixed set of values

The solution

Mimetic scales your rem and em values relative to the browser's viewport whilst respecting the device-pixel-ratio which means:

  • Fonts and other elements can scale when you resize the window
  • The browser's zoom will be able to increase and decrease your pages entities
  • There is no need to create breakpoints beyond the memisis-breakpoint default 1024px/ 64em

Why

Ideally, typography that scales with its surroundings produces more harmonised aesthetics and can upscale automatically without intervention.

How it works

Mimetic scales fonts and any containers that use rem or em units

The mimesis-breakpoint is the viewport width when Mimetic begins to scale your rem and em values

  • Create your responsive layout as normal within the mimesis-breakpoint 0 to 1024px/ 64em
  • px will not scale, so ensure fonts and fixed dimensions you want to scale are in rem or em units (rem should be prefered to avoid compounding)
  • Import the mimetic function and call it early within the head of the document to avoid jittering the first meaningful paint
  • Resize the window past the mimesis-breakpoint to see it in effect

Support

Mimetic supports all evergreen browsers and gracefully falls back to your 1024px (64em) design for unsupported browsers and devices.

Install and use use

<script type="module">
  import mimetic from './mimetic.js'
  mimetic()
</script>

or

npm i mimetic

Detecting the browser's zoom level

Although this is possible, it's recommended to not make your application depend on it. Treat this feature as an enhancement, not a necessity.

mimetic({
  scale: false,
  onZoom: (a,b,c,d) => console.log(a,b,c,d);
});

Options

Below is the list of config options passed as an object:

PropertyValueDescriptionDefault
cutOffWidthString - CSS unitsThe minimal width to disable scaling0
enableScaleBoooleanEnables scaling of relative unitstrue
loadEventString - LoadEvent typenative load event to use before auctioning"DOMContentLoaded"
mobileWidthString - CSS unitsThe minimal width to disable scaling for mobile devices"40em"
onResize({viewportWidth, innerWidth, evalDPR, calculatedDPR, normalizedDPR}) => {}Callback on scale and or zoomundefined
onScale({viewportWidth, innerWidth, evalDPR, calculatedDPR, normalizedDPR}) => {}Callback on resize onlyundefined
onZoom({viewportWidth, innerWidth, evalDPR, calculatedDPR, normalizedDPR}) => {}Callback on zoom onlyundefined
preserveDevicePixelRatioBooleanNormalises the device pixel ratio for high ratio devicesfalse
relativeDesignWidthString - CSS unitsThe width relative to the font size"1024px"
scaleDelayNumber - MillisecondsThe debounced delay to call on resize16

The alternatives

The pursuit of getting fonts to scale to the viewport impeccably is like treading a tightrope. Below are some of the issues encountered when exploring alternative font-scaling libraries during the making of Mimietic.

Inconsistent browser compatibility, difficult to no ability to zoom thus breaking accessibility, doesn't scale padding/ margin/ line-height and other relative dimensions, doesn’t respect the style attributes on elements. Must always specify relative dimensions (padding/ margin)(More maintenance), you can’t specify relative dimensions, scales to a container only, framework dependent, lacks a breakpoint, fonts blur or are no longer being maintained.

Big thanks to BrowserStack for sponsoring the cross-browser & device testing of this project.

MIT © 2024 Julien Etienne

0.7.9

16 days ago

0.7.8

3 months ago

0.7.6

4 months ago

0.7.7

4 months ago

0.7.5

4 months ago

0.7.1

7 years ago

0.7.0

7 years ago

0.6.9

7 years ago

0.6.5

7 years ago

0.6.0

7 years ago

0.5.9

7 years ago

0.5.7

7 years ago

0.5.6

7 years ago

0.5.0

7 years ago