Skip to content

Instantly share code, notes, and snippets.

@rapPayne
Last active September 14, 2023 17:55
Show Gist options
  • Save rapPayne/7d8709ea8d391bbd118f799e63b4f211 to your computer and use it in GitHub Desktop.
Save rapPayne/7d8709ea8d391bbd118f799e63b4f211 to your computer and use it in GitHub Desktop.
React component styled with CSS nesting
/*
** Note the use of CSS nesting. Nesting all styles underneath a class of
** ChangeName ensures they will ONLY be seen in the ChangeName component.
** ๐Ÿ™Œ This is the best of both worlds!
*/
.ChangeName {
border: 1px solid var(--dark1);
padding: 10px;
&>div {
margin: 10px;
&>label {
display: block;
}
&>input {
font-size: 1.1em;
display: block;
}
}
&>button {
color: var(--light1);
background-color: var(--dark1);
}
}
// ๐Ÿ™Œ
// Back to simple! The only change is adding a className to the root and
// importing the CSS file.
import './ChangeName.css';
export const ChangeName = ({ user, update, save }) => {
return (
<section className="ChangeName">
<div>
<label htmlFor="user">User name</label>
<input id="user" onChange={e => update(e.target.value)} value={user} />
</div>
<button onClick={save}>Save</button>
</section>
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment