Vue Truncate by Line (vue-clamp)
September 27, 2020- Tested to work on Vue v2, Vue v3 and Nuxt.js
- Support both truncate only or truncate with toggle (more or less)
Install
npm i --save vue-clamp
For Nuxt.js, edit nuxt.config.js
export default {
build: {
transpile: ['vue-clamp', 'resize-detector']
},
}
Truncate only
<template>
<div>
<v-clamp :max-lines="2" autoresize>{{ description }}</v-clamp>
</div>
</template>
<script>
import VClamp from 'vue-clamp
export default {
data() {
return {
description: "The cactus wren (Campylorhynchus brunneicapillus) is a large wren that is endemic to the deserts of the United States and Mexico. It is the state bird of Arizona. The wren's upperparts are brown with black and white spots and the underparts are cinnamon-buff with a whiter breast; it has striking white eyebrows. The song is loud and raspy. Cactus wrens are ground feeders and eat mainly insects, with some plant material; they can meet their water needs from their diet."
}
}
components: {
VClamp
}
}
</script>
NOTE: Make sure the parent of v-clamp
is not <p>
, as <p>
can’t hold other tags (will trigger The client-side rendered virtual DOM tree is not matching server-rendered content.
). If you need v-clamp
to be <p>
, use tag="p"
.
Truncate with Toggle
<v-clamp :max-lines="2" autoresize >
{{ description }}
<template #after="{ toggle, expanded, clamped }">
<a
href="#"
v-if="expanded || clamped"
class="ml-1 badge badge-light"
@click.prevent="toggle"
>{{ expanded ? 'Less' : 'More' }}</a>
</template>
</v-clamp>
❤️ Is this article helpful?
Buy me a coffee☕ or support my work to keep this space 🖖 and ad-free.
If you can't, do send some 💖 to @d_luaz or help to share this article.
If you can't, do send some 💖 to @d_luaz or help to share this article.
👶 Apps I built
Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.Pixtory App (Alpha) - easily organize photos on your phone into a blog.
暖心芽 (WIP) 🌞❤️🌱 - reminder of hope, warmth, thoughts and feelings (or just quotes).
LuaPass - offline password manager
By Desmond Lua
- algo-trading
- algolia
- analytics
- android
- android-ktx
- android-permission
- android-studio
- apps-script
- bash
- binance
- bootstrap
- bootstrapvue
- chartjs
- chrome
- cloud-functions
- coding-interview
- contentresolver
- coroutines
- crashlytics
- crypto
- css
- dagger2
- datastore
- datetime
- docker
- eslint
- firebase
- firebase-auth
- firebase-hosting
- firestore
- firestore-security-rules
- flask
- fontawesome
- fresco
- git
- github
- glide
- godot
- google-app-engine
- google-cloud-storage
- google-colab
- google-drive
- google-maps
- google-places
- google-play
- google-sheets
- gradle
- html
- hugo
- inkscape
- java
- java-time
- javascript
- jetpack-compose
- jetson-nano
- kotlin
- kotlin-serialization
- layout
- lets-encrypt
- lifecycle
- linux
- logging
- lubuntu
- markdown
- mate
- material-design
- matplotlib
- md5
- mongodb
- moshi
- mplfinance
- mysql
- navigation
- nginx
- nodejs
- npm
- nuxtjs
- nvm
- pandas
- payment
- pip
- pwa
- pyenv
- python
- recylerview
- regex
- room
- rxjava
- scoped-storage
- selenium
- social-media
- ssh
- ssl
- static-site-generator
- static-website-hosting
- sublime-text
- ubuntu
- unit-test
- uwsgi
- viewmodel
- viewpager2
- virtualbox
- vue-chartjs
- vue-cli
- vue-router
- vuejs
- vuelidate
- vuepress
- web-development
- web-hosting
- webpack
- windows
- workmanager
- wsl
- yarn