Gathering detailed insights and metrics for leading-trim
Gathering detailed insights and metrics for leading-trim
Gathering detailed insights and metrics for leading-trim
Gathering detailed insights and metrics for leading-trim
trim-leading-lines
Trim leading lines from a string when they are 100% whitespace or empty.
strip-outer
Strip a substring from the start/end of a string
upath
A proxy to `path`, replacing `\` with `/` for all results (supports UNC paths) & new methods to normalize & join keeping leading `./` and add, change, default, trim file extensions.
@capsizecss/unpack
Unpack capsize font metrics from fonts
Strip out spacing above and below the first and last lines of a text block
npm install leading-trim
Typescript
Module System
Min. Node Version
Node Version
NPM Version
TypeScript (92.85%)
JavaScript (7.15%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
40 Stars
96 Commits
1 Watchers
11 Branches
2 Contributors
Updated on Apr 29, 2025
Latest Version
1.0.2
Package Id
leading-trim@1.0.2
Unpacked Size
21.42 kB
Size
5.93 kB
File Count
10
NPM Version
9.6.0
Node Version
16.17.1
Published on
Apr 06, 2023
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
Strip out spacing above and below the first and last lines of a text block
By default text elements include vertical space based on its line-height
value. The effect of that extra space may be overlooked or worked around, but
when working with precise scales and layout components, there's probably no room
for random spacing going around your text.
leading-trim
is a JavaScript port of EightShapes's
Text Crop mixin
(source). It returns a CSS
styles object ready to be used with any CSS-in-JS library that let's you inject
styles with nested pseudo-elements.
1npm install leading-trim 2 3# or 4 5yarn add leading-trim
leading-trim
exports a set of functions:
leadingTrim
Use it when you're setting your font-family
to a custom @font-face
1import { leadingTrim } from "leading-trim";
2
3leadingTrim({
4 lineHeight: 1.5, // unitless `line-height` that you want for the text
5 reference: { // reference numbers for the `@font-face` you'll use
6 fontSize: 40, // `font-size` in px
7 lineHeight: 1, // unitless `line-height`
8 topCrop: 5, // height to remove from the top in px
9 bottomCrop: 6, // height to remove from the bottom in px
10 },
11 correction: { // (optional) adjust the cropping result
12 top: 1, // a positive value shortens the crop
13 bottom: -1, // a negative value expands the crop
14 },
15});
systemFontLeadingTrim
Use them when you're setting your font-family
to a typical system font stack:
1body: { 2 fontfamily: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen-Sans, Ubuntu, Cantarell, "Helvetica Neue", sans-serif; 3}
The reference
numbers are already set for this one so you don't have to.
Naturally different operative systems will use different fonts, so it's a close
approximation, you can still use correction
for your use-case:
1import { systemFontLeadingTrim } from "leading-trim";
2
3systemFontLeadingTrim({
4 lineHeight: 1.5, // unitless `line-height` that you want for the text
5 correction: { // (optional) adjust the cropping result
6 top: 1, // a positive value shortens the crop
7 bottom: -1, // a negative value expands the crop
8 },
9})
systemMonoFontReference
Same as above but for a monospace font-family
stack:
1body: { 2 fontfamily: SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace; 3}
1import { systemMonoFontReference } from "leading-trim";
2
3systemMonoFontReference({
4 lineHeight: 1.5, // unitless `line-height` that you want for the text
5 correction: { // (optional) adjust the cropping result
6 top: 1, // a positive value shortens the crop
7 bottom: -1, // a negative value expands the crop
8 },
9})
If the reference
numbers were correctly matched, you probably won't need
correction
numbers different than -1
or 1
.
The output of the above functions is a JS object ready to be used in a CSS-in-JS library of your choice:
1{ 2 "display": "block", 3 "lineHeight": 1.5, 4 "&::after": { 5 "marginTop": "calc(-0.375em - 1px + -1px)", 6 }, 7 "&::before": { 8 "marginBottom": "calc(-0.375em - 1px + 1px)", 9 }, 10 "&::before, &::after": { 11 "content": "\\"\\"", 12 "display": "block", 13 "height": 0, 14 "paddingTop": "1px", 15 "width": 0, 16 } 17}
The code above will work with any font size automatically so it's up to you if you want to provide it along or just let the element inherit the one from the parent element.
Check the examples for a more realistic usage.
Disclaimer: My weapon of choice is React so the examples are only based on that, feel free to PR some other examples using other libraries.
reference
numbers (I got the best results using a line height of 1
and a
> 32px
font size)lineHeight
and the reference
numbers from the previous step to
the leadingTrim
functionPass your lineHeight
to the systemFontLeadingTrim
function and inject the styles object that the function outputs using your
prefered CSS-in-JS library.
reference
numbers?To be able to calculate the size of the top and bottom crop for any given
line-height
. It's basicaly a rule of 3 so it needs something to compare.
You'll need different reference numbers for each font-family
you use (not
needed for different font-weights
as font height is not changing in that
case).
The EightShapes's Text Crop mixin tool makes it a really easy to get it working fast.
I started learning about this technique when I saw it applied in SEEK's Braid Design System, since then I've seen other resources that may help you wrap your head around the issue:
There's a
a CSS proposal to address this natively using the leading-trim
property.
This is not trying to pollyfill that property proposal at all, but it does have
a matching purpose:
By using
leading-trim
to strip out the spacing above the cap height and below the alphabetic baseline, centering the box actually centers the text; and does so reliably, regardless of what font is used to render it.
Built with TypeScript using tsdx
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 0/26 approved changesets -- score normalized to 0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
12 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-06-30
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More