Skip to content

Instantly share code, notes, and snippets.

@jamiebuilds
Last active November 13, 2017 11:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jamiebuilds/b6307cec1bbf4b9531a5de46f1a46b30 to your computer and use it in GitHub Desktop.
Save jamiebuilds/b6307cec1bbf4b9531a5de46f1a46b30 to your computer and use it in GitHub Desktop.

stylable-components

CSS for Components

Example

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

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

.button {
  extends: Button;
}

.button:disabled {
  color: gray;
}

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

const Root = styled('div', styles.root);
const Control = styled('button', styles.control);
const Count = styled('span', styles.count);

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