Skip to content

Instantly share code, notes, and snippets.

@bogdanq
Last active December 31, 2019 10:07
Show Gist options
  • Save bogdanq/a39b3d30334edfe30f7c65d50871e423 to your computer and use it in GitHub Desktop.
Save bogdanq/a39b3d30334edfe30f7c65d50871e423 to your computer and use it in GitHub Desktop.
import styled from "styled-components";
const mapPoint = ({ position }) => ({
style: {
top: position.top,
left: position.left
}
});
// api styled позволяет через атрибуты навесить стили
// Такой подход позволит убрать один апдейт стилей в рантайме
const Point = styled.div.attrs(mapPoint)`
position: absolute;
border: 1px solid blue;
width: 50px;
height: 50px;
background: red;
`;
// такой вариант так же уберет лишний апдейт стайледа
const Button = styled.button`
&[data-variant="primary"] {
background: red
}
&[data-variant="secondary"] {
background: green
}
&[data-variant="success"] {
background: yellow
}
`
/**
Usage
const [position, setPosition] = React.useState({ top: 0, left: 0 });
<Point position={position}>Point</Point>
<Button data-variant="primary">click</Button>
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment