Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?

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
You can’t perform that action at this time.