<vue-clamp>
Clamping multiline contents with ease.
# Features
- Clamps contents with max lines and max height. No need to specify line height for text.
- Automatically updates upon layout change.
- The clamped contents can be expanded/collapsed.
- Customizable and responsive content before/after clamped contents.
# Demo
Clamped: No
# Usage
$ npm i --save @boyuai/vue-clamp
For project based on webpack, import
will load the untranspiled ES Module version. Please make sure to add the correct config according to following instructions.
Vue CLI v3
For projects created with Vue CLI 3, remember to add @boyuai/vue-clamp
and resize-detector
into the transpileDependencies
option in vue.config.js
:
module.exports = {
transpileDependencies: ['@boyuai/vue-clamp', 'resize-detector']
}
Vue CLI v2
For Vue CLI 2 with the webpack
template, modify build/webpack.base.conf.js
like this:
{
test: /\.js$/,
loader: 'babel-loader',
- include: [resolve('src'), resolve('test')]
+ include: [
+ resolve('src'),
+ resolve('test'),
+ resolve('node_modules/@boyuai/vue-clamp'),
+ resolve('node_modules/resize-detector')
+ ]
}
Nuxt.js v2
When using in Nuxt.js, remember to add @boyuai/vue-clamp
and resize-detector
into the build.transpile
option in nuxt.config.js
:
module.exports = {
build: {
transpile: ['@boyuai/vue-clamp', 'resize-detector']
}
}
<template>
<v-clamp autoresize :max-lines="3">{{ text }}</v-clamp>
</template>
<script>
import VClamp from '@boyuai/vue-clamp'
export default {
components: {
VClamp
},
data () {
return {
text: 'Some very very long text content.'
}
}
}
</script>
# API
tag: string
The tag name of the generated root element.
Default:
div
autoresize: boolean
Whether to observe the root element's size.
Default:
false
max-lines: number
The max number of lines that can be displayed.
line-height: number
Non-text contents must provide their line-height for clamping.
max-height: number|string
The max height of the root element. Number values are converted to
px
units. String values are used directly as themax-height
CSS property.ellipsis: string
The ellipsis characters displayed when the text is clamped.
Default:
'…'
expanded: boolean
.sync
Whether the clamped area is expanded.
Default:
false
default
The contents to clamp. Only supports pure text or same type html node.
before
Slot scope:
{ expand, collapse, toggle, clamped, expanded }
expand: function(): void
- Expand the clamped contents.collapse: function(): void
- Collapse the expanded contents.toggle: function(): void
- Toggle the expand state of clamped contents.clamped: Boolean
- Whether contents are clamped.expanded: Boolean
- Whether contents are expanded.Content displayed before the clamped contents. Can contain anything.
after
Slot scope: Same as
before
.Content displayed after the clamped contents. Can contain anything.
clampchange
Emitted when clamp state changes.
Callback parameter list:
(clamped: Boolean)