1.0.0-beta • Published 6 months ago

@prop-styles/core v1.0.0-beta

Weekly downloads
-
License
MIT
Repository
-
Last release
6 months ago

@prop-styles/core

The library provides a static method createPropStyles to create Style objects.

npm i @prop-styles/core

Example

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

const style = createPropStyles({ color: 'red', width: 100 }, {
  color: (value) => value ? { key: '--color-primary', value } : null
  // or Use f function to remove null/undefined props
  // color: (value) => transform('--color-primary', value)
})

// style
// { '--color-primary': 'red', width: '100px' }

Methods

createPropStyles(props, mappings)

Create Styles Object

Example

import { createPropStyles, transform } 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) => { key: '--color', value: v }
}) // { width: '100px', '--color', '#fff' }

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

  • @returns Record<string, string>

transform(key, value, strValue)

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

Example

transform('width', 100) // { key: 'width', value: '100' }
transform('width', '100px') // { key: 'width', value: '100px' }
transform('width', 100, '100%') // { key: 'width', value: '100%' }

transform('key', false) // null
transform('key', '') // null
transform('key', undefined) // null
transform('key', null) // null
transform('key', null, 'stringValue') // null
transform('key', true, 'stringValue') // { key: 'key', value: 'stringValue' }
ParamTypesRequiredDescription
keystringyesThe PropMappingHandler Return key or customize key
valueanyyesThe props[prop]'s value
strValuestringnoCustomize the value of PropMappingHandler Return
  • @returns { key: string, value: string } | null

Types

BaseProps

Commonly used CSS properties for components.

csstype Property

PropTypesRequiredDescription
displayProperty.Displaynodisplay
widthnumber/stringnowidth
minWidthnumber/stringnomin-width
maxWidthnumber/stringnomax-width
heightnumber/stringnoheight
minHeightnumber/stringnomin-height
maxHeightnumber/stringnomax-height
flexProperty.Flexnoflex
gapnumber/stringnoflex/grid's gap
columnbooleannoflex-direction
fdProperty.FlexDirectionnoflex-direction
aiProperty.AlignItemsnoalign-items
acProperty.AlignContentnoalign-content
jiProperty.JustifyItemsnojustify-items
jcProperty.JustifyContentnojustify-content
wrapboolean/Property.FlexWrapnoflex-wrap
wsProperty.WhiteSpacenowhite-space
pnumber/stringnopadding
ptnumber/stringnopadding-top
prnumber/stringnopadding-right
pbnumber/stringnopadding-bottom
plnumber/stringnopadding-left
pxnumber/stringnopadding-inline
pynumber/stringnopadding-block
mnumber/stringnomargin
mtnumber/stringnomargin-top
mrnumber/stringnomargin-right
mbnumber/stringnomargin-bottom
mlnumber/stringnomargin-left
mxnumber/stringnomargin-inline
mynumber/stringnomargin-block
radiusstring/numbernoborder-radius
fsstring/numbernofont-size
fwProperty.FontWeightnofont-weight
lhstring/numbernoline-height
colorstringnocolor
bgProperty.Backgroundnobackground
scrollboolean/'x'/'y'noscroll direction
breakWordbooleannotext
borderstring/numbernoborder, border-width, border-color
gtcstring/numbernogrid-template-columns
gtrstring/numbernogrid-template-rows
taProperty.TextAlignnotext-align
positionProperty.Positionnoposition
topstring/numbernotop
rightstring/numbernoright
bottomstring/numbernobottom
leftstring/numbernoleft
zIndexProperty.ZIndexnoz-index
insetstring/numbernoinset
transformProperty.Transformnotransform
interface BaseProps {
  // display
  display?: Property.Display;
  // 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;
  // flex
  flex?: Property.Flex;
  // flex/grid's gap
  gap?: number | string;
  // flex-direction
  column?: boolean;
  // flex-direction
  fd?: Property.FlexDirection;
  // align-items
  ai?: Property.AlignItems;
  // align-content
  ac?: Property.AlignContent;
  // justify-items
  ji?: Property.JustifyItems;
  // justify-content
  jc?: Property.JustifyContent;
  // flex-wrap
  wrap?: boolean | Property.FlexWrap;
  // white-space
  ws?: Property.WhiteSpace;
  // padding
  p?: number | string;
  // padding-top
  pt?: number | string;
  // padding-right
  pr?: number | string;
  // padding-bottom
  pb?: number | string;
  // padding-left
  pl?: number | string;
  // padding-inline
  px?: number | string;
  // padding-block
  py?: number | string;
  // margin
  m?: number | string;
  // margin-top
  mt?: number | string;
  // margin-right
  mr?: number | string;
  // margin-bottom
  mb?: number | string;
  // margin-left
  ml?: number | string;
  // margin-inline
  mx?: number | string;
  // margin-block
  my?: number | string;
  // border-radius
  radius?: string | number;
  // font-size
  fs?: string | number;
  // font-weight
  fw?: Property.FontWeight;
  // line-height
  lh?: string | number;
  // color
  color?: string;
  // background
  bg?: Property.Background;
  // scroll direction
  scroll?: boolean | 'x' | 'y';
  // text
  breakWord?: boolean;
  // border, border-width, border-color
  border?: string | number;
  // grid-template-columns
  gtc?: string | number;
  // grid-template-rows
  gtr?: string | number;
  // text-align
  ta?: Property.TextAlign;
  // position
  position?: Property.Position;
  // top
  top?: string | number;
  // right
  right?: string | number;
  // bottom
  bottom?: string | number;
  // left
  left?: string | number;
  // z-index
  zIndex?: Property.ZIndex;
  // inset
  inset?: string | number;
  // transform
  transform?: Property.Transform;
}

PropMappingHandler

PropMappings processing function, returns { key: string, value: string } | null

PropTypesRequiredDescription
valueany,yes-
propsTyes-
type PropMappingHandler<T extends BaseProps> = (
  value: any,
  props: T
) => PropMappingHandlerReturn | PropMappingHandlerReturn[];

PropMappingHandlerReturn

type PropMappingHandlerReturn = { key: string; value: string } | null;

PropMappings

PropMappingHandler

type PropMappings<T extends BaseProps> = Partial<
  Record<keyof T, PropMappingHandler<T>>
>;

License

MIT License © 2024-Present Capricorncd.

1.0.0-beta

6 months ago

1.0.0-beta05112249

6 months ago

0.0.11

2 years ago

0.0.10

2 years ago

0.0.9

2 years ago

0.0.8

2 years ago

0.0.5

2 years ago

0.0.4

2 years ago

0.0.7

2 years ago

0.0.6

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago