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

@retil/control

v0.1.1

Published

Decoupled styles for your control components

Downloads

47

Readme

@retil/control

Create styled buttons and link components with standard CSS -- including :hover, :focus, etc. -- without coupling your styles to the underlying <button> or <a> tags.

See an introduction with live examples »

This package currently assumes that you're using Styled Components. Want to add Emotion support? Please file an issue!

Installation

npm install -s @retil/control

# or

yarn add @retil/control

Basic Usage

There's two parts to using @retil/control:

  1. Create a styled component to render the control's styles
  2. Add behavior by wrapping it with a control component

1. Create a styled component

You have two options for styling your control: you can use CSS template strings, or style objects with nested selectors.

with template strings

import { active, focus, hover } from '@retil/control'

const ButtonBody = ({ children, ...rest ) => (
  <span {...rest} css={css`
    background-color: palevioletred;
    border: 1px solid palevioletred;
    border-radius: 5px;
    color: white;
    margin: 5px;

    ${active`
      transform: scale(0.99);
    `}

    ${focus`
      outline: 1px solid deepskyblue;
    `}

    ${hover`
      background-color: transparent;
      color: palevioletred;
    `}
  `}>
    {children}
  </span>
)

with objects with nested selectors

TODO

2. Add behavior with a Control Component

import { AControl } from '@retil/control'

export const App = () => (
  <AControl href='/'>
    <ButtonBody>
      Home
    </ButtonBody>
  </AControl>
)

Full API

Control components

These components accept the same properties as the underlying element with the same name, but render without any default styles.

Control components exported by @retil/control include:

  • <AControl>
  • <ButtonControl>

Props

All components accept the following props, in addition to the props accepted by the underlying component:

  • active (boolean)
  • disabled (boolean)
  • focus (boolean)
  • hover (boolean)
  • inline (boolean)

Styles

All components render as display: flex by default. They can be switched to display: inline-flex by passing an inline prop.

Components also have the following flexbox styles applied by default -- these can be changed via className, css or style prop.

flex-direction: column;

align-items: stretch;
justify-content: stretch;

Custom controls with the higher order control() function

To create your own custom Control component, you'll first need a Styled Component (as returned directly from the Style Components styled() function). Then, you can turn it into a control component by wrapping it with control.

Here's an example -- this is the exact code used to create the ButtonControl component exported by @retil/control

import styled from 'styled-components'
import { control, resetButtonCSS } from '@retil/control'

export const ButtonControl = control(
  styled.button`
    ${resetButtonCSS}
  `,
)

The control() function also accepts a function as a second argument, which lets you define the selectors which will be made available to child components. Use this to override the default active, disabled, focus and hover selectors -- or to provide custom selectors of your own.

const StyledButtonControl = styled.button`
  ${resetButtonCSS}
`

// Default behavior
export const ButtonControl = control(
  StyledButtonControl,
  (selectorName: string) => `${Component}:${selectorName}`
)

Template helpers

The following template strings allow can be interpolated within a styled component's css to add styled which respond to events on the control itself.

  • active
  • disabled
  • focus
  • hover

Example usage:

import { hover } from '@retil/control'

const ButtonBody = styled.span`
  opacity: 1;
  transition: opacity 100ms ease-out;
  
  ${hover`
    opacity: 0.9;
  `}
`

Custom template helpers with createTemplateHelper()

If you've added your own custom selectors by passing a second argument to control(), you may also want to create your own custom template helpers.

import { createTemplateHelper } from '@retil/control'

const hover = createTemplateHelper('hover')

useControlContext()

Returns the control context, which is internally stored on the retilControl prop of the styled components theme.

It has the following shape:

export interface Control {
  // Returns a selector which can be interpolated into styles. The default
  // controls support `active`, `focus` and `hover` selectors, but this can
  // be configured by creating custom controls.
  getSelector: (name: string) => string
  forceSelectors: {
    [selectorName: string]: boolean
  }
}

Types

The following TypeScript types are exported:

  • AControlProps
  • ButtonControlsProps
  • Control

CSS reset strings

The CSS strings used to reset the default styles on <a> and <button> are exported for your convenience:

import {
  resetACSS,
  resetButtonCSS,
} from '@retil/control`

License

MIT licensed.