A Twind extension that provides utilities for visually truncating text after a fixed number of lines.
Based on @tailwindcss/line-clamp.
Install from npm:
# Using npm
npm install @twind/line-clamp
# Using Yarn
yarn add @twind/line-clamp
Use the lineClamp(lines)
function to specify how many lines of text should be visible before truncating:
import { lineClamp } from '@twind/line-clamp'
document.body.innerHTML = `
<p class="${tw`max-w-xs mx-auto ${lineClamp(3)}`}">
Et molestiae hic earum repellat aliquid est doloribus delectus. Enim illum odio porro ut omnis dolor debitis natus. Voluptas possimus deserunt sit delectus est saepe nihil. Qui voluptate possimus et quia. Eligendi voluptas voluptas dolor cum. Rerum est quos quos id ut molestiae fugit.
</p>
`
To remove any line-clamping, use lineClamp('none')
:
document.body.innerHTML = `
<p class="${tw`max-w-xs mx-auto ${lineClamp(3)} md:${lineClamp('none')}`}">
Et molestiae hic earum repellat aliquid est doloribus delectus. Enim illum odio porro ut omnis dolor debitis natus. Voluptas possimus deserunt sit delectus est saepe nihil. Qui voluptate possimus et quia. Eligendi voluptas voluptas dolor cum. Rerum est quos quos id ut molestiae fugit.
</p>
`
Note that the
lineClamp(lines)
set other properties likedisplay
andoverflow
in addition to-webkit-line-clamp
which are not unset bylineClamp('none')
, so depending on what you are trying to achieve you may need to explicitly override those properties with utilities likeflex
oroverflow-visible
as well.
Add the plugin to plugins of your setup call:
import { lineClamp } from '@twind/line-clamp'
setup({
plugins: {
'line-clamp': lineClamp,
},
})
Use the line-clamp-{n}
utilities to specify how many lines of text should be visible before truncating:
<p class="line-clamp-3">
Et molestiae hic earum repellat aliquid est doloribus delectus. Enim illum odio porro ut omnis
dolor debitis natus. Voluptas possimus deserunt sit delectus est saepe nihil. Qui voluptate
possimus et quia. Eligendi voluptas voluptas dolor cum. Rerum est quos quos id ut molestiae fugit.
</p>
To remove any line-clamping, use line-clamp-none
:
<p class="line-clamp-3 md:line-clamp-none">
Et molestiae hic earum repellat aliquid est doloribus delectus. Enim illum odio porro ut omnis
dolor debitis natus. Voluptas possimus deserunt sit delectus est saepe nihil. Qui voluptate
possimus et quia. Eligendi voluptas voluptas dolor cum. Rerum est quos quos id ut molestiae fugit.
</p>
A lineClamp
section is added to the theme. Its values are used for looking up the value. If no value us found the given parameter is used.
setup({
theme: {
lineClamp: {
card: '5',
},
},
})
tw(lineClamp('card'))
// => -webkit-line-clamp:5
// Not found in theme => use as is
tw(lineClamp('7'))
// => -webkit-line-clamp:7