0.0.9 • Published 1 year ago

@prop-styles/vue v0.0.9

Weekly downloads
-
License
MIT
Repository
-
Last release
1 year ago

@prop-styles/vue

Process CSS-related properties in Props so that they can generate the element style.

npm i @prop-styles/vue

App.vue

<script setup lang="ts">
import { usePropStyles, f, type VueBaseProps } from '@prop-styles/vue'

interface Props extends VueBaseProps {
  customProp?: unknown
}

const props = defineProps<Props>()

const { style } = usePropStyles(props, {
  // Custom prop mapping handler
  customProp: (v: Props['customProp']) => f('custom-prop', v, 'default value used when v is null/false')
})
</script>

<template>
  <div :style="{style}"></div>
</template>
<App width="100" radius="12 12 0 12" marginTop="20" />
// <div style="width:100px;border-radius:12px 12px 0 12px;margin-top:20px;"></div>

Methods

createPropStyles(props, mappings)

Create Styles Object

Example

import { createPropStyles, f } from '@prop-styles/core'

const props = { width: 100, color: '#fff' }

createPropStyles(props) // { width: '100px', color, '#fff' }

// Use custom Mapping handler
createPropStyles(props, {
  // custom mapping handler
  color: (v) => ['--color', v]
}) // { width: '100px', '--color', '#fff' }

// Use f function to remove null/undefined props
createPropStyles(props, {
  color: (v) => f('--color', v)
}) // { width: '100px', '--color', '#fff' }
ParamTypesRequiredDescription
propsTyesBaseProps
mappingsPropMappings<T>noPropMappings
  • @generic T extends BaseProps

  • @returns Record<string, string>

f(key, value, strValue)

Alias and abbreviation of formatReturn.

ParamTypesRequiredDescription
keyKyesThe PropMappingHandler Return key or customize key
valueVyesThe props[prop]'s value
strValuestringnoCustomize the value of PropMappingHandler Return
  • @generic K extends string, V

  • @returns [key: string, val: string] | null

formatReturn(key, value, strValue)

Used for PropMappingHandler processing. When value is null/undefined/''/false, return null, otherwise return the specified value.

Example

f('width', 100) // ['width', '100']
f('width', '100px') // ['width', '100px']
f('width', 100, '100%') // ['width', '100%']

f('key', false) // null
f('key', '') // null
f('key', undefined) // null
f('key', null) // null
f('key', null, 'stringValue') // null
f('key', true, 'stringValue') // ['key', 'stringValue']
ParamTypesRequiredDescription
keyKyesThe PropMappingHandler Return key or customize key
valueVyesThe props[prop]'s value
strValuestringnoCustomize the value of PropMappingHandler Return
  • @generic K extends string, V

  • @returns [key: string, val: string] | null

usePropStyles(props, mappings)

Convert component properties to Style key-value pair objects

ParamTypesRequiredDescription
propsTyesComponent properties
mappingsPropMappings<T>noPropMappings
  • @generic T extends BaseProps

  • @returns UsePropStylesReturn

Types

BaseProps

Commonly used CSS properties for components.

csstype Property

PropTypesRequiredDescription
styleanynostyle
widthnumber/stringnowidth
minWidthnumber/stringnomin-width
maxWidthnumber/stringnomax-width
heightnumber/stringnoheight
minHeightnumber/stringnomin-height
maxHeightnumber/stringnomax-height
flexbooleannodisplay: flex
gridbooleannodisplay: grid
inlineFlexbooleannodisplay: inline-flex
inlineBlockbooleannodisplay: inline-block
inlinebooleannodisplay: inline
gapnumber/stringnoflex/grid's gap
columnbooleannoflex-direction
alignProperty.AlignItemsnoalign-items
alignItemsProperty.AlignItemsnoalign-items
aiProperty.AlignItemsnoalign-items
alignContentProperty.AlignContentnoalign-content
acProperty.AlignContentnoalign-content
justifyProperty.JustifyContentnojustify-content
justifyContentProperty.JustifyContentnojustify-content
jcProperty.JustifyContentnojustify-content
justifyItemsProperty.JustifyItemsnojustify-items
jiProperty.JustifyItemsnojustify-items
wrapboolean/Property.FlexWrapnoflex-wrap
nowrapbooleannowhite-space: nowrap
whiteSpaceProperty.WhiteSpacenowhite-space
paddingnumber/stringnopadding
pnumber/stringnopadding
paddingTopnumber/stringnopadding-top
ptnumber/stringnopadding-top
paddingBottomnumber/stringnopadding-bottom
pbnumber/stringnopadding-bottom
paddingLeftnumber/stringnopadding-left
plnumber/stringnopadding-left
paddingRightnumber/stringnopadding-right
prnumber/stringnopadding-right
paddingInlinenumber/stringnopadding-inline
pxnumber/stringnopadding-inline
paddingBlocknumber/stringnopadding-block
pynumber/stringnopadding-block
marginnumber/stringnomargin
mnumber/stringnomargin
marginTopnumber/stringnomargin-top
mtnumber/stringnomargin-top
marginBottomnumber/stringnomargin-bottom
mbnumber/stringnomargin-bottom
marginLeftnumber/stringnomargin-left
mlnumber/stringnomargin-left
marginRightnumber/stringnomargin-right
mrnumber/stringnomargin-right
marginInlinenumber/stringnomargin-inline
mxnumber/stringnomargin-inline
marginBlocknumber/stringnomargin-block
mynumber/stringnomargin-block
radiusstring/numbernoborder-radius
fontSizestring/numbernofont-size
fsstring/numbernofont-size
lineHeightstring/numbernoline-height
lhstring/numbernoline-height
colorstringnocolor
backgroundProperty.Backgroundnobackground
bgProperty.Backgroundnobackground
scrollboolean/'x'/'y'noscroll direction
breakWordbooleannotext
boldbooleannofont-weight: bold
thinbooleannofont-weight: 500
fontWeightProperty.FontWeightnofont-weight
fwProperty.FontWeightnofont-weight
borderstring/numbernoborder, border-width, border-color
tempColumnsstring/numbernogrid-template-columns
gtcstring/numbernogrid-template-columns
tempRowsstring/numbernogrid-template-rows
gtrstring/numbernogrid-template-rows
textAlignProperty.TextAlignnotext-align
taProperty.TextAlignnotext-align
positionProperty.Positionnoposition
topstring/numberno-
tstring/numbernotop
rightstring/numbernoright
rstring/numbernoright
bottomstring/numbernobottom
bstring/numbernobottom
leftstring/numbernoleft
lstring/numbernoleft
zIndexProperty.ZIndexnoz-index
zProperty.ZIndexnoz-index
insetstring/numbernoinset
transformProperty.Transformnotransform
tfProperty.Transformnotransform
interface BaseProps {
  // style
  style?: any
  // width
  width?: number | string
  // min-width
  minWidth?: number | string
  // max-width
  maxWidth?: number | string
  // height
  height?: number | string
  // min-height
  minHeight?: number | string
  // max-height
  maxHeight?: number | string
  // display: flex
  flex?: boolean
  // display: grid
  grid?: boolean
  // display: inline-flex
  inlineFlex?: boolean
  // display: inline-block
  inlineBlock?: boolean
  // display: inline
  inline?: boolean
  // flex/grid's gap
  gap?: number | string
  // flex-direction
  column?: boolean
  // align-items
  align?: Property.AlignItems
  // align-items
  alignItems?: Property.AlignItems
  // align-items
  ai?: Property.AlignItems
  // align-content
  alignContent?: Property.AlignContent
  // align-content
  ac?: Property.AlignContent
  // justify-content
  justify?: Property.JustifyContent
  // justify-content
  justifyContent?: Property.JustifyContent
  // justify-content
  jc?: Property.JustifyContent
  // justify-items
  justifyItems?: Property.JustifyItems
  // justify-items
  ji?: Property.JustifyItems
  // flex-wrap
  wrap?: boolean | Property.FlexWrap
  // white-space: nowrap
  nowrap?: boolean
  // white-space
  whiteSpace?: Property.WhiteSpace
  // padding
  padding?: number | string
  // padding
  p?: number | string
  // padding-top
  paddingTop?: number | string
  // padding-top
  pt?: number | string
  // padding-bottom
  paddingBottom?: number | string
  // padding-bottom
  pb?: number | string
  // padding-left
  paddingLeft?: number | string
  // padding-left
  pl?: number | string
  // padding-right
  paddingRight?: number | string
  // padding-right
  pr?: number | string
  // padding-inline
  paddingInline?: number | string
  // padding-inline
  px?: number | string
  // padding-block
  paddingBlock?: number | string
  // padding-block
  py?: number | string
  // margin
  margin?: number | string
  // margin
  m?: number | string
  // margin-top
  marginTop?: number | string
  // margin-top
  mt?: number | string
  // margin-bottom
  marginBottom?: number | string
  // margin-bottom
  mb?: number | string
  // margin-left
  marginLeft?: number | string
  // margin-left
  ml?: number | string
  // margin-right
  marginRight?: number | string
  // margin-right
  mr?: number | string
  // margin-inline
  marginInline?: number | string
  // margin-inline
  mx?: number | string
  // margin-block
  marginBlock?: number | string
  // margin-block
  my?: number | string
  // border-radius
  radius?: string | number
  // font-size
  fontSize?: string | number
  // font-size
  fs?: string | number
  // line-height
  lineHeight?: string | number
  // line-height
  lh?: string | number
  // color
  color?: string
  // background
  background?: Property.Background
  // background
  bg?: Property.Background
  // scroll direction
  scroll?: boolean | 'x' | 'y'
  // text
  breakWord?: boolean
  // font-weight: bold
  bold?: boolean
  // font-weight: 500
  thin?: boolean
  // font-weight
  fontWeight?: Property.FontWeight
  // font-weight
  fw?: Property.FontWeight
  // border, border-width, border-color
  border?: string | number
  // grid-template-columns
  tempColumns?: string | number
  // grid-template-columns
  gtc?: string | number
  // grid-template-rows
  tempRows?: string | number
  // grid-template-rows
  gtr?: string | number
  // text-align
  textAlign?: Property.TextAlign
  // text-align
  ta?: Property.TextAlign
  // position
  position?: Property.Position
  top?: string | number
  // top
  t?: string | number
  // right
  right?: string | number
  // right
  r?: string | number
  // bottom
  bottom?: string | number
  // bottom
  b?: string | number
  // left
  left?: string | number
  // left
  l?: string | number
  // z-index
  zIndex?: Property.ZIndex
  // z-index
  z?: Property.ZIndex
  // inset
  inset?: string | number
  // transform
  transform?: Property.Transform
  // transform
  tf?: Property.Transform
}

PropMappingHandler

PropMappings processing function, returns [key: string, val: string] | null

PropTypesRequiredDescription
valueT[keyof T],yes-
propsTyes-
type PropMappingHandler<T extends BaseProps> = (
  value: T[keyof T],
  props: T
) => PropMappingHandlerReturn

PropMappingHandlerReturn

PropMappingHandler's returns

type PropMappingHandlerReturn = [key: string, val: string] | null

PropMappings

PropMappingHandler

PropTypesRequiredDescription
key: keyof TPropMappingHandler<T>yes-
type PropMappings<T extends BaseProps> = {
  [key: keyof T]: PropMappingHandler<T>
}

UsePropStylesReturn

PropTypesRequiredDescription
styleComputedRef<StyleValue[]>yes-
interface UsePropStylesReturn {
  style: ComputedRef<StyleValue[]>
}

VueBaseProps

PropTypesRequiredDescription
styleanynostyle
widthnumber/stringnowidth
minWidthnumber/stringnomin-width
maxWidthnumber/stringnomax-width
heightnumber/stringnoheight
minHeightnumber/stringnomin-height
maxHeightnumber/stringnomax-height
flexbooleannodisplay: flex
gridbooleannodisplay: grid
inlineFlexbooleannodisplay: inline-flex
inlineBlockbooleannodisplay: inline-block
inlinebooleannodisplay: inline
gapnumber/stringnoflex/grid's gap
columnbooleannoflex-direction
alignProperty.AlignItemsnoalign-items
alignItemsProperty.AlignItemsnoalign-items
aiProperty.AlignItemsnoalign-items
alignContentProperty.AlignContentnoalign-content
acProperty.AlignContentnoalign-content
justifyProperty.JustifyContentnojustify-content
justifyContentProperty.JustifyContentnojustify-content
jcProperty.JustifyContentnojustify-content
justifyItemsProperty.JustifyItemsnojustify-items
jiProperty.JustifyItemsnojustify-items
wrapboolean/Property.FlexWrapnoflex-wrap
nowrapbooleannowhite-space: nowrap
whiteSpaceProperty.WhiteSpacenowhite-space
paddingnumber/stringnopadding
pnumber/stringnopadding
paddingTopnumber/stringnopadding-top
ptnumber/stringnopadding-top
paddingBottomnumber/stringnopadding-bottom
pbnumber/stringnopadding-bottom
paddingLeftnumber/stringnopadding-left
plnumber/stringnopadding-left
paddingRightnumber/stringnopadding-right
prnumber/stringnopadding-right
paddingInlinenumber/stringnopadding-inline
pxnumber/stringnopadding-inline
paddingBlocknumber/stringnopadding-block
pynumber/stringnopadding-block
marginnumber/stringnomargin
mnumber/stringnomargin
marginTopnumber/stringnomargin-top
mtnumber/stringnomargin-top
marginBottomnumber/stringnomargin-bottom
mbnumber/stringnomargin-bottom
marginLeftnumber/stringnomargin-left
mlnumber/stringnomargin-left
marginRightnumber/stringnomargin-right
mrnumber/stringnomargin-right
marginInlinenumber/stringnomargin-inline
mxnumber/stringnomargin-inline
marginBlocknumber/stringnomargin-block
mynumber/stringnomargin-block
radiusstring/numbernoborder-radius
fontSizestring/numbernofont-size
fsstring/numbernofont-size
lineHeightstring/numbernoline-height
lhstring/numbernoline-height
colorstringnocolor
backgroundProperty.Backgroundnobackground
bgProperty.Backgroundnobackground
scrollboolean/'x'/'y'noscroll direction
breakWordbooleannotext
boldbooleannofont-weight: bold
thinbooleannofont-weight: 500
fontWeightProperty.FontWeightnofont-weight
fwProperty.FontWeightnofont-weight
borderstring/numbernoborder, border-width, border-color
tempColumnsstring/numbernogrid-template-columns
gtcstring/numbernogrid-template-columns
tempRowsstring/numbernogrid-template-rows
gtrstring/numbernogrid-template-rows
textAlignProperty.TextAlignnotext-align
taProperty.TextAlignnotext-align
positionProperty.Positionnoposition
topstring/numberno-
tstring/numbernotop
rightstring/numbernoright
rstring/numbernoright
bottomstring/numbernobottom
bstring/numbernobottom
leftstring/numbernoleft
lstring/numbernoleft
zIndexProperty.ZIndexnoz-index
zProperty.ZIndexnoz-index
insetstring/numbernoinset
transformProperty.Transformnotransform
tfProperty.Transformnotransform
classanyno-
interface VueBaseProps extends BaseProps {
  class?: any
}

License

MIT License © 2024-Present Capricorncd.

0.0.9

1 year ago

0.0.8

1 year ago

0.0.7

1 year ago

0.0.5

1 year ago

0.0.4

1 year ago

0.0.6

1 year ago

0.0.3

1 year ago

0.0.2

1 year ago

0.0.1

1 year ago