Skip to content

Instantly share code, notes, and snippets.

@geelen
Forked from jamiebuilds/stylable-components.md
Last active November 13, 2017 11:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save geelen/007c65662467cb06b3077b01e9a5cdf9 to your computer and use it in GitHub Desktop.
Save geelen/007c65662467cb06b3077b01e9a5cdf9 to your computer and use it in GitHub Desktop.

stylable-components

CSS for Components

Example

/* Counter.css */
@import Button from './Button.css';

div.root {
  padding: 0.5rem;
  border: 1px solid #eee;
  border-radius: 3px;
}

button.button {
  extends: Button;
}

.button:disabled {
  color: gray;
}

span.count {
  font-size: 2em;
}
// Counter.js
import React from 'react';
import styled from 'stylable-components';
import * as styles from './Counter.css';

const { Root, Control, Count } = styled(styles);

type Props = {
  count: number,
  min: number,
  max: number,
  onChange: number => mixed,
};

export default function Counter(props: Props) {
  let isMin = props.count <= props.min;
  let isMax = props.count >= props.max;

  return (
    <Root>
      <Control
        onClick={() => isMin || props.onChange(Math.max(props.count - 1, props.min))}
        disabled={isMin}>
        Decrement
      </Control>
      <Count>
        {props.count}
      </Count>
      <Control
        onClick={() => isMax || props.onChange(Math.min(props.count + 1, props.max))}
        disabled={isMax}>
        Increment
      </Control>
    </Root>
  );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment