bootstrap-vue
Version:
BootstrapVue, with more than 85 custom components, over 45 plugins, several custom directives, and over 300 icons, provides one of the most comprehensive implementations of Bootstrap v4 components and grid system for Vue.js. With extensive and automated W
60 lines (56 loc) • 1.71 kB
JavaScript
import Vue from '../../utils/vue'
import { mathAbs } from '../../utils/math'
import { toFloat } from '../../utils/number'
import normalizeSlotMixin from '../../mixins/normalize-slot'
// --- Constants ---
const NAME = 'BAspect'
const CLASS_NAME = 'b-aspect'
const RX_ASPECT = /^\d+(\.\d*)?[/:]\d+(\.\d*)?$/
const RX_SEPARATOR = /[/:]/
// --- Main Component ---
export const BAspect = /*#__PURE__*/ Vue.extend({
name: NAME,
mixins: [normalizeSlotMixin],
props: {
aspect: {
// Accepts a number (i.e. `16 / 9`, `1`, `4 / 3`)
// Or a string (i.e. '16/9', '16:9', '4:3' '1:1')
type: [Number, String],
default: '1:1'
},
tag: {
type: String,
default: 'div'
}
},
computed: {
padding() {
const aspect = this.aspect
let ratio = 1
if (RX_ASPECT.test(aspect)) {
// Width and/or Height can be a decimal value below `1`, so
// we only fallback to `1` if the value is `0` or `NaN`
const [width, height] = aspect.split(RX_SEPARATOR).map(v => toFloat(v) || 1)
ratio = width / height
} else {
ratio = toFloat(aspect) || 1
}
return `${100 / mathAbs(ratio)}%`
}
},
render(h) {
const $sizer = h('div', {
staticClass: `${CLASS_NAME}-sizer flex-grow-1`,
style: { paddingBottom: this.padding, height: 0 }
})
const $content = h(
'div',
{
staticClass: `${CLASS_NAME}-content flex-grow-1 w-100 mw-100`,
style: { marginLeft: '-100%' }
},
[this.normalizeSlot('default')]
)
return h(this.tag, { staticClass: `${CLASS_NAME} d-flex` }, [$sizer, $content])
}
})