Gathering detailed insights and metrics for @capsizecss/core
Gathering detailed insights and metrics for @capsizecss/core
Gathering detailed insights and metrics for @capsizecss/core
Gathering detailed insights and metrics for @capsizecss/core
npm install @capsizecss/core
@capsizecss/metrics@3.4.0
Published on 13 Nov 2024
@capsizecss/metrics@3.3.0
Published on 15 Sept 2024
@capsizecss/unpack@2.3.0
Published on 29 Jul 2024
@capsizecss/metrics@3.2.0
Published on 23 May 2024
@capsizecss/core@4.1.2
Published on 08 May 2024
@capsizecss/unpack@2.2.0
Published on 05 May 2024
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
1,530 Stars
222 Commits
38 Forks
11 Watching
11 Branches
18 Contributors
Updated on 27 Nov 2024
TypeScript (92.48%)
CSS (6.01%)
JavaScript (1.44%)
HTML (0.05%)
Shell (0.03%)
Cumulative downloads
Total Downloads
Last day
-33.9%
5,979
Compared to previous day
Last week
-13.2%
36,484
Compared to previous week
Last month
3.2%
185,723
Compared to previous month
Last year
73.5%
1,999,055
Compared to previous year
1
1
Capsize makes the sizing and layout of text as predictable as every other element on the screen.
Using font metadata, text can now be sized according to the height of its capital letters while trimming the space above capital letters and below the baseline.
1npm install @capsizecss/core
createStyleObject
Returns a CSS-in-JS style object.
createStyleObject
passing the relevant options.1import { createStyleObject } from '@capsizecss/core'; 2 3const capsizeStyles = createStyleObject({ 4 fontSize: 16, 5 leading: 24, 6 fontMetrics: { 7 capHeight: 700, 8 ascent: 1058, 9 descent: -291, 10 lineGap: 0, 11 unitsPerEm: 1000, 12 }, 13});
Note: It is recommended that you install the @capsizecss/metrics package and import the metrics from there:
1import { createStyleObject } from '@capsizecss/core'; 2import arialMetrics from '@capsizecss/metrics/arial'; 3 4const capsizeStyles = createStyleObject({ 5 fontSize: 16, 6 leading: 24, 7 fontMetrics: arialMetrics, 8});
See the fontMetrics option documented below for more ways to obtain these metrics.
css
prop.1<div 2 css={{ 3 // fontFamily: '...' etc, 4 ...capsizeStyles, 5 }} 6> 7 My capsized text 🛶 8</div>
⚠️ Note: It is not recommended to apply further layout-related styles to the same element, as this will risk interfering with the styles used for the trim. Instead consider using a nested element.
createStyleString
Returns a CSS string that can be inserted into a style
tag or appended to a stylesheet.
createStyleString
passing the relevant options.1import { createStyleString } from '@capsizecss/core'; 2import arialMetrics from '@capsizecss/metrics/arial'; 3 4const capsizedStyleRule = createStyleString('capsizedText', { 5 fontSize: 16, 6 leading: 24, 7 fontMetrics: arialMetrics, 8});
style
element and apply the specified class name.1document.write(` 2 <style type="text/css"> 3 ${capsizedStyleRule} 4 </style> 5 <div class="capsizedText"> 6 My capsized text 🛶 7 </div> 8`);
⚠️ Note: It is not recommended to apply further layout-related styles to the same element, as this will risk interfering with the styles used for the trim. Instead consider using a nested element.
Capsize supports two methods of defining the size of text, capHeight
and fontSize
.
NOTE: You should only ever pass one or the other, not both.
capHeight: <number>
Sets the height of the capital letters to the defined value. Defining typography in this way makes aligning to a grid or with other elements, e.g. icons, a breeze.
fontSize: <number>
Setting the font size allows you to get all the benefits of the white space trimming, while still specifying an explicit font-size
for your text. This can be useful when needed to match a concrete design spec or fitting into an existing product.
Capsize supports two mental models for specifying line height, lineGap
and leading
. If you pass neither the text will follow the default spacing of the specified font, e.g. line-height: normal
.
NOTE: You should only ever pass one or the other, not both.
lineGap: <number>
Sets the number of pixels between lines, as measured between the baseline and cap height of the next line.
leading: <number>
Sets the line height to the provided value as measured from the baseline of the text. This aligns the web with how typography is treated in design tools.
This metadata is extracted from the metrics tables inside the font itself. There are a number of ways to find this information:
If using a Google Font or system font, install the @capsizecss/metrics package and import the metrics by name. For example:
1import arialMetrics from '@capsizecss/metrics/arial';
If using a font from a file, install the @capsizecss/unpack package and extract the metrics from the font file directly. For example:
1import { fromFile } from '@capsizecss/unpack'; 2 3const metrics = await fromFile(filePath);
Or, use the Capsize website to find these by selecting a font and referencing Metrics
tab in step 3.
The core package also provides a few other metrics-based features for improving typography on the web:
Creates metrics-based @font-face
declarations to improve the alignment of font family fallbacks, which can dramatically improve the Cumulative Layout Shift metric for sites that depend on a web font.
Consider the following example, where the desired web font is Lobster, falling back to Helvetica Neue
and then Arial
, e.g. font-family: Lobster, 'Helvetica Neue', Arial
.
createFontStack
from the core package:1import { createFontStack } from '@capsizecss/core';
1import lobster from '@capsizecss/metrics/lobster'; 2import helveticaNeue from '@capsizecss/metrics/helveticaNeue'; 3import arial from '@capsizecss/metrics/arial';
font-family
CSS property.1const { fontFamily, fontFaces } = createFontStack([ 2 lobster, 3 helveticaNeue, 4 arial, 5]);
The returned value contains the generated font face declarations as well as the computed fontFamily
with the appropriately ordered font aliases.
The returned values can be templated into a stylesheet or a style
block. Here is an example handlebars template:
1<style type="text/css"> 2 .heading { 3 font-family: {{ fontFamily }} 4 } 5 6 {{ fontFaces }} 7</style>
This will produce the following CSS:
1.heading { 2 font-family: Lobster, 'Lobster Fallback: Helvetica Neue', 3 'Lobster Fallback: Arial', 'Helvetica Neue', Arial; 4} 5 6@font-face { 7 font-family: 'Lobster Fallback: Helvetica Neue'; 8 src: local('Helvetica Neue'); 9 ascent-override: 115.1741%; 10 descent-override: 28.7935%; 11 size-adjust: 86.8251%; 12} 13@font-face { 14 font-family: 'Lobster Fallback: Arial'; 15 src: local('Arial'); 16 ascent-override: 113.5679%; 17 descent-override: 28.392%; 18 size-adjust: 88.053%; 19}
If working with a CSS-in-JS library, the returned fontFaces
can be provided as a JavaScript style object by providing styleObject
as a fontFaceFormat
option.
Here is an example using Emotion:
1import { Global } from '@emotion/core'; 2 3const { fontFaces, fontFamily } = createFontStack( 4 [lobster, helveticaNeue, arial], 5 { 6 fontFaceFormat: 'styleObject', 7 }, 8); 9 10export const App = () => ( 11 <> 12 <Global styles={fontFaces} /> 13 <p css={{ fontFamily }}>...</p> 14 </> 15);
Also useful as a source for further manipulation given it is a data structure that can be iterated over or extended.
font-face
propertiesAdditional properties can be added to the generated @font-face
declarations via the fontFaceProperties
option:
1const { fontFamily, fontFaces } = createFontStack( 2 [lobster, helveticaNeue, arial], 3 { 4 fontFaceProperties: { 5 fontDisplay: 'swap', 6 }, 7 }, 8);
This will result in the following additions to the declarations:
1 @font-face { 2 font-family: 'Lobster Fallback: Helvetica Neue'; 3 src: local('Helvetica Neue'); 4 ascent-override: 115.1741%; 5 descent-override: 28.7935%; 6 size-adjust: 86.8251%; 7+ font-display: swap; 8 } 9 @font-face { 10 font-family: 'Lobster Fallback: Arial'; 11 src: local('Arial'); 12 ascent-override: 113.5679%; 13 descent-override: 28.392%; 14 size-adjust: 88.053%; 15+ font-display: swap; 16 }
[!NOTE] Passing any of the metric override CSS properties will be ignored as they are calculated by Capsize. However, the
size-adjust
property is accepted to support fine-tuning the override for particular use cases. This can be used to finesse the adjustment for specific text, or to disable the adjustment by setting it to100%
.
For languages that use different unicode subsets, e.g. Thai, the fallbacks need to be scaled accordingly, as the scaling is based on character frequency in written language.
A fallback font stack can be generated for a supported subset by specifying subset
as an option:
1const { fontFamily, fontFaces } = createFontStack([lobster, arial], { 2 subset: 'thai', 3});
[!TIP] Need support for a different unicode subset? Either create an issue or follow the steps outlined in the
generate-weightings
script and open a PR.
Returns all the information required to create leading trim styles for a specific font size given the provided font metrics. This is useful for integrations with different styling solutions.
Accepts the same options as createStyleObject and createStyleString.
1import { precomputeValues } from '@capsizecss/core'; 2import arialMetrics from '@capsizecss/metrics/arial'; 3 4const capsizeValues = precomputeValues({ 5 fontSize: 16, 6 leading: 24, 7 fontMetrics: arialMetrics, 8}); 9 10// => { 11// fontSize: string, 12// lineHeight: string, 13// capHeightTrim: string, 14// baselineTrim: string, 15//}
Return the rendered cap height for a specific font size given the provided font metrics.
1import { getCapHeight } from '@capsizecss/core'; 2import arialMetrics from '@capsizecss/metrics/arial'; 3 4const actualCapHeight = getCapHeight({ 5 fontSize: 24, 6 fontMetrics: arialMetrics, 7}); 8 9// => number
To make the retrieval of font metrics easy, Capsize provides the @capsizecss/metrics
package containing all the required data for both system and Google fonts.
1npm install @capsizecss/metrics
See the package for documentation.
If you are using a custom font or one not included in the @capsizecss/metrics
package, Capsize provides the @capsizecss/unpack
package to extract the required data either via a URL or from a local file.
1npm install @capsizecss/unpack
See the package for documentation.
MIT.
No vulnerabilities found.
Reason
all changesets reviewed
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
5 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 4
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
35 existing vulnerabilities detected
Details
Score
Last Scanned on 2024-11-18
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