npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ui-box

v5.4.1

Published

Blazing Fast React UI Primitive

Downloads

58,364

Maintainers

yshkpryshkprpriyanshispriyanshisjbasiglio-segmentjbasiglio-segmentpraguptapraguptasbhodiwalsbhodiwalsahilsharma0709sahilsharma0709farhan0581farhan0581jkusa_segmentjkusa_segmentharksinghharksingharjunbhandagearjunbhandagey.yuy.yuslenin-twilioslenin-twilioskondamuriskondamurinithan-twilionithan-twiliopraveenugiripraveenugiribnakkinabnakkinahema-bahirwani-segmenthema-bahirwani-segmentsandhya16sandhya16nemery-segmentnemery-segmentighorelaighorelamansarimansaribhavanki-segmentbhavanki-segmentar7devar7devvaibhavnandavaibhavnandanikumarsegmentnikumarsegmentitsarijitrayitsarijitrayssunejassunejaazhaotwilioazhaotwilioldelossantosldelossantoscjo2cjo2achandrashekaranachandrashekaranaparna.singhalaparna.singhalynguyenynguyentimmyzsearcytimmyzsearcyigracheva-twilioigracheva-twilioyashnit-segmentyashnit-segmentnanotimmnanotimmsrivig21srivig21chenchensegmentcomchenchensegmentcomsanket.mishrasanket.mishralweimersegmentlweimersegmentrcheedhallarcheedhallajxin_twiliojxin_twilioodoren_segmentodoren_segmentaditi.raveeshaditi.raveeshdltnbrks-segmentdltnbrks-segmentsai-patanjalisai-patanjalihimanshuphhimanshuphalecjacobs-segmentalecjacobs-segmentsshaikh_segmentsshaikh_segmentjohn.lee1100john.lee1100dazu70dazu70brian.aguirrebrian.aguirrepooja.patilpooja.patilsegment_fansegment_fannlubchenconlubchencojsh-wujsh-wunithin-bennynithin-bennypoojasegmentpoojasegmentebru.odokebru.odokbannapplebannapplerodhilton_twiliorodhilton_twilioguthriesegmentguthriesegmentsrishti-nemasrishti-nemacdelaomartinezcdelaomartinezmiguelpdiaz8miguelpdiaz8sundareswar.jayakumarsundareswar.jayakumargbatragbatraspencerattickspencerattickakodankiryakodankirynanette.ranesnanette.ranesgsolis_segmentgsolis_segmentnitinpm432nitinpm432amigandhiamigandhisegmentseansegmentseanhmohanram_seghmohanram_segjrupasinghejrupasinghedevthaledevthalesmccoy-twiliosmccoy-twilioseg-leonelsanchesseg-leonelsanchesjibrangjibrangsethgrid_segmentsethgrid_segmentlight-bringer-blrlight-bringer-blraubreysineaubreysineed-twilion-npmed-twilion-npmharsh-joshi99harsh-joshi99irfan.ali.segmentirfan.ali.segmentkbhargavaram-sgkbhargavaram-sgneedcaffeineneedcaffeinenat-gridnat-gridwlumsegmentwlumsegmentmoyara2moyara2bala.singareddybala.singareddyakash.gautam07akash.gautam07preetyppreetypviveksainaneesegmentviveksainaneesegmentmsarafmsarafkjoerreskjoerresrokatyalrokatyalainatancincoainatancincoanton-vylushchakanton-vylushchaksowjanyasegmentsowjanyasegmentalayvoraalayvoratw-dgarciatw-dgarciaparag.pandaparag.pandablangtwilioblangtwilioryanrouleau-segmentryanrouleau-segmenttwjosiahtwjosiahmcullenmeyermcullenmeyerdavid.anusontarangkul.segmentdavid.anusontarangkul.segmentmckern_segmentmckern_segmentsegment-adminsegment-adminnainy.agrawalnainy.agrawaltdibaccotdibaccosudojatinsudojatinnageshgolemnageshgolembrandonheyer-segmentbrandonheyer-segmentalfrimpongalfrimpongdobrin.ganevdobrin.ganevankit.gupta.unthinkableankit.gupta.unthinkablemarinheromarinherobenattwiliobenattwiliobharath.boregowdabharath.boregowdaconniechenconniechensungju.jinsungju.jinpooyajpooyajyli119yli119ea_segmentea_segmentemilyjiaemilyjiakx-segmentkx-segmentxinghao.huangxinghao.huangharsh.vardhanharsh.vardhanjoe.ayoub.segmentjoe.ayoub.segmentgkochar123gkochar123rollcoderollcodeariel.silvestriariel.silvestricherylj-segmentcherylj-segmentimmanojimmanojaaronklishaaronklishmichelrmichelrmaneesh.dharma29maneesh.dharma29brianhumphreystwiliobrianhumphreystwiliojfehrman.segmentjfehrman.segmentjoetessyjoetessypmuninpmuninjalexy12jalexy12jbandi-twiliojbandi-twilioprayansh-twilioprayansh-twiliodominicbarnesdominicbarnesbrandon.scott-segmentbrandon.scott-segmentbgillanbgillanphillip.thomasphillip.thomasricardo.rossiricardo.rossiforgetfulfellowforgetfulfellowfauzy.yyfauzy.yymayur-pitalemayur-pitaledbaik-twilio-segmentdbaik-twilio-segmentseg-rustybaileyseg-rustybaileytanya.gupta.segmenttanya.gupta.segmentpmiller-twiliopmiller-twilionevermore2022nevermore2022aishikawakiaishikawakicsayusocsayusomcoulibalimcoulibalishupadhyayshupadhyayjahood-twiliojahood-twiliosaisagarkappaganthulasaisagarkappaganthularmukundanrmukundanarubiochavezarubiochavezshuvrajit9904shuvrajit9904s3gm3nts3gm3ntama0223ama0223tbrennanjtbrennanjdangmai-segmentdangmai-segmentshraddha-twilioshraddha-twiliosa-jsootersa-jsooterenyi.asonyeenyi.asonyeafsha-nazim-segafsha-nazim-segmschaszbergermschaszbergerlnambalnambavaradarajan-twvaradarajan-twseanhan-segmentseanhan-segmentreplateroreplaterosayan-das-insayan-das-injusteenjusteenjgabe13jgabe13meg1000meg1000funlufunluashwitha.bgashwitha.bgwhaider_twiliowhaider_twiliotcgilberttcgilbertkevinburkesegmentkevinburkesegmentfelttripfelttripprabhnoor1997prabhnoor1997akashyap91akashyap91clintz.segclintz.segkarimkeshwanikarimkeshwaniwally.tgwally.tgrhall-twiliorhall-twilioyash-twilioyash-twiliobrookstaylorjrbrookstaylorjrshayan-golafshanishayan-golafshanilerahulramlerahulrammugelstadmugelstadrrivera-segmentrrivera-segmentsethnutetwiliosethnutetwiliomanali-bhosalemanali-bhosalechtoombschtoombssethsegmentsethsegmenteric-hydeeric-hydeelmoselyeeelmoselyeemichaelghsegmichaelghsegjayakrishnannairjayakrishnannairlateefatlateefatmaryam.sharifmaryam.sharifwdbettswdbettsryanligonryanligonsindhusegmentsindhusegmentlfdelossantoslfdelossantosaramakrishnanaramakrishnanvbatanovvbatanovlluque-twiliolluque-twiliojair.avilesjair.avilespmaidpmaidsong4yousong4youpeterdemartinipeterdemartiniemmy.byrneemmy.byrnevincen7tranvincen7trandean-huynhdean-huynhcdignam-segmentcdignam-segmentabhinavsurekaabhinavsurekaarunlalam-segmentarunlalam-segmentneeharikakondipatineeharikakondipatisimpixelatedsimpixelatedchihchun-twiliochihchun-twilioacharles14acharles14jyim008jyim008fhalim-segmentfhalim-segmentcfreecfreehjoonpmhjoonpmceline-segmentceline-segmentpmcanseco-segmentpmcanseco-segmentmasiramasiraamillet89amillet89cholt002cholt002av-segmentav-segmentaghotikaraghotikarvikrant-segmentvikrant-segmentlarryatsegmentlarryatsegmentscruwys1scruwys1kyliepedersenkyliepedersenjinaparkjinaparksegmentiosegmentiorajulvaderarajulvaderalpediredlalpediredlan2parkon2parkotyson_segmenttyson_segmentbgamwellbgamwellsalolivaressalolivareserikdwerikdwchenxiangzhangchenxiangzhangmericssonmericssonprayansh-segmenttprayansh-segmenttjeremylarkinjeremylarkinbsneedbsneeddanieljackinsdanieljackinssegment-sethsegment-sethjames9446james9446priscilla.giattipriscilla.giattinlsunnlsundrew-thompsondrew-thompsonsegment-jsinghsegment-jsinghandrius-segmentandrius-segmentvalerieernstvalerieernstgilomergilomermarcelopvmarcelopveric.rognereric.rognerkdharaiyakdharaiyajon.anderson-at-segment.comjon.anderson-at-segment.comstacy.songstacy.songrexatsegmentrexatsegmentnickaguilarnickaguilarbradenbeckerbradenbeckerreneewangreneewangdan.laskydan.laskysam.tapiasam.tapiavikramkumar19vikramkumar19mpriyad25mpriyad25jeremy.parkerjeremy.parkersmidgessmidgesrownorownojeroenransijnjeroenransijn

Keywords

react

Readme

📦 ui-box is a low level CSS-in-JS solution that focuses on being simple, fast and extensible. All CSS properties are set using simple React props, which allows you to easily create reusable components that can be enhanced with additional CSS properties. This is very useful for adding things like margins to components, which would normally require adding non-reusable wrapper elements/classes.

Install

yarn add ui-box
# or
npm install --save ui-box

Usage

import Box from 'ui-box'

function Button(props) {
  return <Box is="button" padding="10px" background="red" {...props} />
}

function Example() {
  return (
    <Button disabled margin="10px">
      Hi
    </Button>
  )
}

The above example component renders a red, disabled <button> with margins.

API

Box (default export)

is

Type: string or React component type Default: 'div'

Lets you change the underlying element type. You can pass either a string to change the DOM element type, or a React component type to inherit another component. The component just needs to accept a className prop to work. A good example is inheriting the react-router Link component. E.g:

<Box is={Link} to="/login">
  Login
</Box>
clearfix

Type: boolean

Utility property for easily adding clearfix styles to the element.

className

Type: string

The className prop you know and love. Internally it gets enhanced with additional class names for the CSS properties you specify.

selectors

Type: object<selector: string, props: CssProps>

This prop allows you to define selectors and custom styles to apply when the selector condition is met. This can be used to create richer element interactions, such as hover or focus states, without the use of another css-in-js library.

<Box is="a" selectors={{ '&:hover': { cursor: 'pointer', textDecoration: 'underline' } }}>
  Hello world
</Box>
CSS properties

All of these CSS properties are supported. You can pass either a string or a number (which gets converted to a px value). The shorthand properties with repeated values only accept a single value, e.g. margin="10px" works but margin="10px 20px" does not. You can use the x/y props (e.g. marginX/marginY) to achieve the same thing.

  • alignContent
  • alignItems
  • alignSelf
  • animation
  • animationDelay
  • animationDirection
  • animationDuration
  • animationFillMode
  • animationIterationCount
  • animationName
  • animationPlayState
  • animationTimingFunction
  • background
  • backgroundBlendMode
  • backgroundClip
  • backgroundColor
  • backgroundImage
  • backgroundOrigin
  • backgroundPosition
  • backgroundRepeat
  • backgroundSize
  • border
  • borderBottom
  • borderBottomColor
  • borderBottomLeftRadius
  • borderBottomRightRadius
  • borderBottomStyle
  • borderBottomWidth
  • borderColor
  • borderLeft
  • borderLeftColor
  • borderLeftStyle
  • borderLeftWidth
  • borderRadius
  • borderRight
  • borderRightColor
  • borderRightStyle
  • borderRightWidth
  • borderStyle
  • borderTop
  • borderTopColor
  • borderTopLeftRadius
  • borderTopRightRadius
  • borderTopStyle
  • borderTopWidth
  • borderWidth
  • bottom
  • boxShadow
  • boxSizing - Set to border-box by default.
  • clear
  • color
  • columnGap
  • content
  • cursor
  • display
  • fill
  • flex
  • flexBasis
  • flexDirection
  • flexFlow
  • flexGrow
  • flexShrink
  • flexWrap
  • float
  • font
  • fontFamily
  • fontSize
  • fontStyle
  • fontVariant
  • fontWeight
  • gap
  • grid
  • gridArea
  • gridAutoColumns
  • gridAutoFlow
  • gridAutoRows
  • gridColumn
  • gridColumnEnd
  • gridColumnGap
  • gridColumnStart
  • gridGap
  • gridRow
  • gridRowEnd
  • gridRowGap
  • gridRowStart
  • gridTemplate
  • gridTemplateAreas
  • gridTemplateColumns
  • gridTemplateRows
  • height
  • justifyContent
  • justifyItems
  • justifySelf
  • left
  • letterSpacing
  • lineHeight
  • listStyle
  • listStyleImage
  • listStylePosition
  • listStyleType
  • margin
  • marginBottom
  • marginLeft
  • marginRight
  • marginTop
  • marginX - Sets marginLeft and marginRight to the same value.
  • marginY - Sets marginTop and marginBottom to the same value.
  • maxHeight
  • maxWidth
  • minHeight
  • minWidth
  • opacity
  • order
  • outline
  • overflow
  • overflowX
  • overflowY
  • padding
  • paddingBottom
  • paddingLeft
  • paddingRight
  • paddingTop
  • paddingX - Sets paddingLeft and paddingRight to the same value.
  • paddingY - Sets paddingTop and paddingBottom to the same value.
  • placeContent
  • placeItems
  • placeSelf
  • pointerEvents
  • position
  • resize
  • right
  • rowGap
  • stroke
  • strokeDasharray
  • strokeDashoffset
  • strokeLinecap
  • strokeMiterlimit
  • strokeWidth
  • textAlign
  • textDecoration
  • textOverflow
  • textShadow
  • textTransform
  • top
  • transform
  • transformOrigin
  • transition
  • transitionDelay
  • transitionDuration
  • transitionProperty
  • transitionTimingFunction
  • userSelect
  • verticalAlign
  • visibility
  • whiteSpace
  • width
  • wordBreak
  • wordWrap
  • zIndex
Other props

All other props passed through to the underlying DOM element / React component.

extractStyles()

Returns a { cache, styles } object which contains the cache entries and rendered styles for server rendering. The styles can be output in a <style> tag or an external stylesheet (however you want). The cache should be passed to hydrate() on the client-side before mounting the app. Also useful for doing snapshot unit testing (make sure to call clearStyles() after each test though).

hydrate(cache)

Hydrates the cache using the cache value returned from extractStyles(). This is used to prevent needing to recalculate all the class names and re-render all the styles on page load when server rendering.

clearStyles()

Clears the cache and removes the rendered styles. Mainly useful for resetting the global state when using extractStyles() in unit tests.

splitProps(props, keys)

Utility function for filtering out props based on an array of keys. Returns an { matchedProps, remainingProps } object.

props

Type: object

keys

Type: array[string]

splitBoxProps(props)

Utility function for filtering out Box props. Returns an { matchedProps, remainingProps } object.

props

Type: object

keyframes(name, timeline)

Function for generating an animation keyframe name and injecting the provided styles into the stylesheet. The timeline object is in the shape of { 'from' | 'to' | [0-100]: CssProps } which define the styles to apply at each position of the animation. Returns the generated name for use with the animation or animationName props.

import Box, { keyframes } from 'ui-box'

const openAnimation = keyframes('openAnimation', {
  from: {
    opacity: 0,
    transform: 'translateY(-120%)'
  },
  to: {
    transform: 'translateY(0)'
  }
})

const AnimatedBox = () => <Box animation={`${openAnimation} 240ms cubic-bezier(0.175, 0.885, 0.320, 1.175)`} />

// Equivalent using individual props:
const AnimatedBox = () => (
  <Box
    animationName={openAnimation}
    animationDuration={240}
    animationTimingFunction="cubic-bezier(0.175, 0.885, 0.320, 1.175)"
  />
)

propTypes

Object of all the Box CSS property propTypes.

propNames

Array of all the Box CSS property names.

propAliases

Array of the Box CSS property aliases (the shorthand properties).

propEnhancers

Object of all the CSS property enhancers (the methods that generate the class name and styles for each property).

Enhancer groups

These enhancer groups are also exported. They're all objects with { propTypes, propAliases, propEnhancers } properties. They're mainly useful for if you want to inherit a subset of the Box CSS propTypes in your own components.

  • animation
  • background
  • borderRadius
  • borders
  • boxShadow
  • dimensions
  • flex
  • interaction
  • layout
  • list
  • opacity
  • overflow
  • position
  • spacing
  • svg
  • text
  • transform
  • transition

Classname prefix

By default ui-box uses ub- as the classname prefix before all ui-box generated classnames. You can alter this by using setClassNamePrefix('whatever-you-want-'). Note that the delimiter is included in the prefix... this is to support backwards compatibility with the old classnames (< v3), which you can achieve using something like this:

import { setClassNamePrefix } from 'ui-box'
setClassNamePrefix('📦')

Safe hrefs

By default ui-box ensures that urls use safe protocols when passed to an element. We built this functionality into ui-box to protect the end users of the products you are building. You can opt-out of this by using configureSafeHref({enabled?: boolean, origin?: string}). This allows you to configure which protocols are acceptable (http:, https:, mailto:, tel:, and data:) and that the correct rel values are added (noopener, noreferrer(for external links)).

import { configureSafeHref } from 'ui-box'
configureSafeHref({
  enabled: true // the default behavior
})
import { configureSafeHref } from 'ui-box'
configureSafeHref({
  enabled: true
  origin: 'https://app.segmentio.com',
})

Additionally you can override the behavior on an individual component basis using the prop allowUnsafeHref

<Box is="a" href="javascript:alert('hi')" allowUnsafeHref>
  This is unsafe
</Box>

Server side rendering

To render the styles on the server side just use ReactDOMServer.renderToString() as usual and then call the extractStyles() method retrieve the rendered styles and cache. The styles can then be output to a <style> tag or an external stylesheet. The cache data should be passed to the hydrate() method on the client side before you call ReactDOM.hydrate().

For example:

'use strict'
const React = require('react')
const ReactDOMServer = require('react-dom/server')
const { default: Box, extractStyles } = require('.')

const element = React.createElement(Box, { margin: '10px', color: 'red' }, 'hi')

const html = ReactDOMServer.renderToString(element)
const { styles, cache } = extractStyles()

const page = `
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>Page</title>
    <style>
      ${styles}
    </style>
  </head>
  <body>
    <main id="root">
      ${html}
    </main>
    <script type="application/json" id="ui-box-cache">
      ${JSON.stringify(cache)}
    </script>
  </body>
</html>
`
console.log(page)

Development

  • yarn dev starts the development Storybook at http://localhost:9009/.
  • yarn test runs the linter, unit tests and code coverage.
  • yarn ava -w runs the unit tests in watch mode.
  • yarn ava -u updates the snapshot tests.
  • yarn build transpiles the JavaScript files.
  • yarn release releases a new version (requires np to be installed globally).

License

ui-box is released under the MIT license.

Copyright © 2017 Segment.io, Inc.