文章目录
- col
- row
看源码时候做的笔记。如有错误请指出!
col
col有属性如下:(与文档一致)
export const colProps = buildProps({
/**
* @description custom element tag
*/
tag: {
type: String,
default: 'div',
},
/**
* @description number of column the grid spans
*/
span: {
type: Number,
default: 24,
},
/**
* @description number of spacing on the left side of the grid
*/
offset: {
type: Number,
default: 0,
},
/**
* @description number of columns that grid moves to the left
*/
pull: {
type: Number,
default: 0,
},
/**
* @description number of columns that grid moves to the right
*/
push: {
type: Number,
default: 0,
},
/**
* @description `<768px` Responsive columns or column props object
*/
xs: {
type: definePropType<ColSize>([Number, Object]),
default: () => mutable({} as const),
},
/**
* @description `≥768px` Responsive columns or column props object
*/
sm: {
type: definePropType<ColSize>([Number, Object]),
default: () => mutable({} as const),
},
/**
* @description `≥992px` Responsive columns or column props object
*/
md: {
type: definePropType<ColSize>([Number, Object]),
default: () => mutable({} as const),
},
/**
* @description `≥1200px` Responsive columns or column props object
*/
lg: {
type: definePropType<ColSize>([Number, Object]),
default: () => mutable({} as const),
},
/**
* @description `≥1920px` Responsive columns or column props object
*/
xl: {
type: definePropType<ColSize>([Number, Object]),
default: () => mutable({} as const),
},
} as const)
根据部分属性生成类名:
const colKls = computed(() => {
const classes: string[] = []
const pos = ['span', 'offset', 'pull', 'push'] as const
/* 遍历props中的相关属性,
如果该属性的值是数字,
则会生成相应的类名
*/
pos.forEach((prop) => {
const size = props[prop]
if (isNumber(size)) {
if (prop === 'span') classes.push(ns.b(`${props[prop]}`))
else if (size > 0) classes.push(ns.b(`${prop}-${props[prop]}`))
}
})
const sizes = ['xs', 'sm', 'md', 'lg', 'xl'] as const
sizes.forEach((size) => {
if (isNumber(props[size])) {
classes.push(ns.b(`${size}-${props[size]}`))
} else if (isObject(props[size])) {
Object.entries(props[size]).forEach(([prop, sizeProp]) => {
classes.push(
prop !== 'span'
? ns.b(`${size}-${prop}-${sizeProp}`)
: ns.b(`${size}-${sizeProp}`)
)
})
}
})
// this is for the fix
if (gutter.value) {
classes.push(ns.is('guttered'))
}
return [ns.b(), classes]
})
写在class中:
<component :is="tag" :class="colKls" :style="style">
<slot />
</component>
col中注入一个rowContextKey,如果没有就默认为ref(0),解构出gutter。
gutter是一个row的属性,表示栅格间隔。
const { gutter } = inject(rowContextKey, { gutter: computed(() => 0) })
rowContextKey在row中provide。
row
提供rowContextKey:
const props = defineProps(rowProps)
const gutter = computed(() => props.gutter)
provide(rowContextKey, {
gutter,
})
左右margin是gutter间隔/2。
const style = computed(() => {
const styles: CSSProperties = {}
if (!props.gutter) {
return styles
}
styles.marginRight = styles.marginLeft = `-${props.gutter / 2}px`
return styles
})
row和col感觉都挺简单的,样式文件的话,是以import形式写的。就没看了。