styled-components, React uygulamalarında komponent bazlı stil oluşturmayı sağlayan bir CSS-in-JS kütüphanesidir. Bu yöntemle CSS doğrudan komponentlerin içine gömülür ve stil yönetimi daha modüler, okunabilir ve tekrar kullanılabilir hale gelir.
Başlamak için:
npm i styled-components
styled.tag
ile bir HTML elementi stil verilebilir.onClick
gibi eventler eklenebilir.import styled from "styled-components";
const H1 = styled.h1`
font-size: 30px;
font-weight: 600;
background-color: yellow;
`;
const Button = styled.button`
font-size: 1.4rem;
padding: 1.2rem 1.6rem;
font-weight: 500;
border-radius: 7px;
background-color: purple;
color: white;
`;
const App = () => (
<div>
<H1>The Wild Oasis</H1>
<Button onClick={() => alert("Alert!")}>Check In</Button>
</div>
);
export default App;
attrs
ile HTML Özellikleri ve Input Kullanımıattrs
metodu ile bileşenlere varsayılan HTML özellikleri eklenebilir.type="file"
ile input bileşeniconst FileInput = styled.input.attrs({ type: "file" })`
border: none;
`;
as
Prop'u ile HTML Elementini Değiştirmeas
prop'u sayesinde stil verilmiş bir bileşen farklı bir HTML tag ile render edilebilir.<Heading as="h1">The Wild Oasis</Heading>