extensions/amp-fit-text/1.0/README.md
Determines the best font size to fit all of a given text content within the available space.
The expected content for Bento Fit Text is text or other inline content, but it can also contain non-inline content.
You must include each Bento component's required CSS library to guarantee proper loading and before adding custom styles. Or use the light-weight pre-upgrade styles available inline. See Layout and style.
npm install @bentoproject/fit-text
import {defineElement as defineBentoFitText} from '@bentoproject/fit-text';
defineBentoFitText();
<script><script type="module" src="https://cdn.ampproject.org/bento.mjs" crossorigin="anonymous"></script>
<script nomodule src="https://cdn.ampproject.org/bento.js" crossorigin="anonymous"></script>
<script type="module" src="https://cdn.ampproject.org/v0/bento-fit-text-1.0.mjs" crossorigin="anonymous"></script>
<script nomodule src="https://cdn.ampproject.org/v0/bento-fit-text-1.0.js" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://cdn.ampproject.org/v0/bento-fit-text-1.0.css" crossorigin="anonymous">
<!DOCTYPE html>
<html>
<head>
<script
type="module"
async
src="https://cdn.ampproject.org/bento.mjs"
></script>
<script nomodule src="https://cdn.ampproject.org/bento.js"></script>
<script
type="module"
async
src="https://cdn.ampproject.org/v0/bento-fit-text-1.0.mjs"
></script>
<script
nomodule
async
src="https://cdn.ampproject.org/v0/bento-fit-text-1.0.js"
></script>
<link
rel="stylesheet"
type="text/css"
href="https://cdn.ampproject.org/v0/bento-fit-text-1.0.css"
/>
</head>
<body>
<bento-fit-text id="my-fit-text">
Lorem ipsum dolor sit amet, has nisl nihil convenire et, vim at aeque
inermis reprehendunt.
</bento-fit-text>
</body>
</html>
If the content of the bento-fit-text overflows the available space, even with a min-font-size specified, the overflowing content is cut off and hidden. WebKit and Blink-based browsers show ellipsis for overflowing content.
In the following example, we specified a min-font-size of 40, and added more content inside the bento-fit-text element. This causes the content to exceed the size of its fixed block parent, so the text is truncated to fit the container.
<div style="width: 300px; height: 300px; background: #005af0; color: #fff">
<bento-fit-text min-font-size="40">
Lorem ipsum dolor sit amet, has nisl nihil convenire et, vim at aeque
inermis reprehendunt. Lorem ipsum dolor sit amet, has nisl nihil convenire
et, vim at aeque inermis reprehendunt
</bento-fit-text>
</div>
Each Bento component has a small CSS library you must include to guarantee proper loading without content shifts. Because of order-based specificity, you must manually ensure that stylesheets are included before any custom styles.
<link
rel="stylesheet"
type="text/css"
href="https://cdn.ampproject.org/v0/bento-fit-text-1.0.css"
/>
Alternatively, you may also make the light-weight pre-upgrade styles available inline:
<style>
bento-fit-text {
display: block;
overflow: hidden;
position: relative;
}
</style>
The bento-fit-text component has a defined layout size type. To ensure the component renders correctly, be sure to apply a size to the component and its immediate children (slides) via a desired CSS layout (such as one defined with height, width, aspect-ratio, or other such properties):
bento-fit-text {
height: 100px;
width: 100%;
}
While overflowing content is visually truncated to fit the container, note that it's still present in the document. Do not rely on the overflow behaviour to simply "stuff" large amounts of content in your pages - while visually it may look appropriate, it may lead to the page becoming overly verbose to users of assistive technologies (such as screen readers), as for these users all the truncated content will still be read/announced in full.
The attributes for <bento-fit-text> can be configured to use different options based on a media query.
min-font-sizeSpecifies the minimum font size in pixels as an integer that the bento-fit-text can use.
max-font-sizeSpecifies the maximum font size in pixels as an integer that the bento-fit-text can use.
Programmatically changing an attribute value will automatically update the element.
<!--% example %--><!DOCTYPE html>
<html>
<head>
<script
type="module"
async
src="https://cdn.ampproject.org/bento.mjs"
></script>
<script nomodule src="https://cdn.ampproject.org/bento.js"></script>
<script
type="module"
async
src="https://cdn.ampproject.org/v0/bento-fit-text-1.0.mjs"
></script>
<script
nomodule
async
src="https://cdn.ampproject.org/v0/bento-fit-text-1.0.js"
></script>
<link
rel="stylesheet"
type="text/css"
href="https://cdn.ampproject.org/v0/bento-fit-text-1.0.css"
/>
</head>
<body>
<bento-fit-text id="my-fit-text">
Lorem ipsum dolor sit amet, has nisl nihil convenire et, vim at aeque
inermis reprehendunt.
</bento-fit-text>
<div class="buttons" style="margin-top: 8px">
<button id="font-button">Change max-font-size</button>
<button id="content-button">Change content</button>
</div>
<script>
(async () => {
const fitText = document.querySelector('#my-fit-text');
await customElements.whenDefined('bento-fit-text');
// set up button actions
document.querySelector('#font-button').onclick = () =>
fitText.setAttribute('max-font-size', '40');
document.querySelector('#content-button').onclick = () =>
(fitText.textContent = 'new content');
})();
</script>
</body>
</html>
npm install @bentoproject/fit-text
import React from 'react';
import {BentoFitText} from '@bentoproject/fit-text/react';
import '@bentoproject/fit-text/styles.css';
function App() {
return (
<BentoFitText>
Lorem ipsum dolor sit amet, has nisl nihil convenire et, vim at aeque
inermis reprehendunt.
</BentoFitText>
);
}
The BentoFitText component has a defined layout size type. To ensure the component renders correctly, be sure to apply a size to the component and its immediate children (slides) via a desired CSS layout (such as one defined with height, width, aspect-ratio, or other such properties). These can be applied inline:
<BentoFitText style={{width: 300, height: 100}}>
Lorem ipsum dolor sit amet, has nisl nihil convenire et, vim at aeque inermis
reprehendunt.
</BentoFitText>
Or via className:
<BentoFitText className="custom-styles">
Lorem ipsum dolor sit amet, has nisl nihil convenire et, vim at aeque inermis
reprehendunt.
</BentoFitText>
.custom-styles {
height: 100px;
width: 100%;
}
minFontSizeSpecifies the minimum font size in pixels as an integer that the bento-fit-text can use.
maxFontSizeSpecifies the maximum font size in pixels as an integer that the bento-fit-text can use.