惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

月光博客
月光博客
人人都是产品经理
人人都是产品经理
C
Check Point Blog
大猫的无限游戏
大猫的无限游戏
小众软件
小众软件
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
博客园 - 聂微东
Help Net Security
Help Net Security
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
L
LINUX DO - 最新话题
Cloudbric
Cloudbric
博客园 - Franky
阮一峰的网络日志
阮一峰的网络日志
O
OpenAI News
V2EX - 技术
V2EX - 技术
H
Hacker News: Front Page
Hacker News: Ask HN
Hacker News: Ask HN
Google DeepMind News
Google DeepMind News
PCI Perspectives
PCI Perspectives
爱范儿
爱范儿
Hacker News - Newest:
Hacker News - Newest: "LLM"
Martin Fowler
Martin Fowler
S
Schneier on Security
Webroot Blog
Webroot Blog
Know Your Adversary
Know Your Adversary
云风的 BLOG
云风的 BLOG
H
Help Net Security
I
InfoQ
Simon Willison's Weblog
Simon Willison's Weblog
雷峰网
雷峰网
J
Java Code Geeks
V
Visual Studio Blog
MyScale Blog
MyScale Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
C
CERT Recently Published Vulnerability Notes
Google Online Security Blog
Google Online Security Blog
Spread Privacy
Spread Privacy
有赞技术团队
有赞技术团队
The GitHub Blog
The GitHub Blog
Engineering at Meta
Engineering at Meta
V
V2EX
Apple Machine Learning Research
Apple Machine Learning Research
www.infosecurity-magazine.com
www.infosecurity-magazine.com
A
About on SuperTechFans
S
SegmentFault 最新的问题
H
Hackread – Cybersecurity News, Data Breaches, AI and More
TaoSecurity Blog
TaoSecurity Blog
WordPress大学
WordPress大学
S
Securelist
P
Proofpoint News Feed

Kent C. Dodds Blog

Implementing Hybrid Semantic + Lexical Search Simplifying Containers with Cloudflare Sandboxes Migrating to Workspaces and Nx Offloading FFmpeg with Cloudflare Building Semantic Search on my Content Helping YOU ask ME questions with AI How I used Cursor to Migrate Frameworks The Dow's Start on the Covenant Path 2025 in Review The next chapter: EpicAI.pro AI is taking your job How I increased my visibility Launching Epic Web 2023 in Review Stop Being a Junior RSC with Dan Abramov and Joe Savona Live Stream Fixing a Memory Leak in a Production Node.js App 2022 in Review My Car Accident I Migrated from a Postgres Cluster to Distributed SQLite with LiteFS I'm building EpicWeb.dev A review of my time at Remix Remix: The Yang to React's Yin How I help you build better websites Why I Love Remix The State Initializer Pattern How to React ⚛️ Get a catch block error message with TypeScript Building an awesome image loading experience How Remix makes CSS clashes predictable Introducing the new kentcdodds.com How I built a modern website in 2021 How to use React Context effectively Static vs Unit vs Integration vs E2E Testing for Frontend Apps The Testing Trophy and Testing Classifications Array reduce vs chaining vs for loop Don't Solve Problems, Eliminate Them Super Simple Start to Remix Super Simple Start to ESModules in Node.js JavaScript Pass By Value Function Parameters How to write a Constrained Identity Function (CIF) in TypeScript How to optimize your context value How to write a React Component in TypeScript TypeScript Function Syntaxes Listify a JavaScript Array Build vs Buy: Component Libraries edition Using fetch with TypeScript Wrapping React.useState with TypeScript Define function overload types with TypeScript 2020 in Review Business and Engineering alignment Hi, thanks for reaching out to me 👋 useEffect vs useLayoutEffect Super simple start to Firebase functions Super simple start to Netlify functions Super Simple Start to css variables Favor Progress Over Pride in Open Source Testing Implementation Details How getting into Open Source has been awesome for me useState lazy initialization and function updates Use ternaries rather than && in JSX Application State Management with React Use react-error-boundary to handle errors in React JavaScript to Know for React How I structure Express apps What open source project should I contribute to? When I follow TDD AHA Programming 💡 How I Record Educational Videos Should I write a test or fix a bug? Stop mocking fetch Intentional Career Building Improve test error messages of your abstractions Tracing user interactions with React Eliminate an entire category of bugs with a few simple tools Common mistakes with React Testing Library Super Simple Start to React Stop using client-side route redirects The State Reducer Pattern with React Hooks Function forms Replace axios with a simple custom fetch wrapper How to test custom React hooks React Production Performance Monitoring Should I useState or useReducer? Stop using isLoading booleans Make Your Test Fail Make your own DevTools An Argument for Automation Fix the "not wrapped in act(...)" warning Super Simple Start to ESModules in the Browser Implementing a simple state machine library in JavaScript 2010s Decade in Review Why users care about how you write code Why I avoid nesting closures Don't call a React function component Why your team needs TestingJavaScript.com Inversion of Control Understanding React's key prop How to Enable React Concurrent Mode Profile a React App for Performance
Introducing glamorous 💄
Kent C. Dodds 🏹 @kentcdodds · 2017-04-04 · via Kent C. Dodds Blog

I was building something for my product at PayPal (blog post maybe forthcoming) and got tired of writing components like this:

const styles = glamor.css({
	fontSize: 20,
	textAlign: 'center',
})
function MyStyledDiv({ className = '', ...rest }) {
	return <div className={`${styles} ${className}`} {...rest} />
}

So I decided to try out styled-components because the hype-train was strong 🚂. I REALLY liked it:

Ok, I was a styled-components hold-out (I really like JS objects). But I've been trying 💅 out for just 30 minutes... And I LOVE IT! 😍😍😍💅😍😍😍

11 99

It allowed me to write that same component like this:

const MyStyledDiv = styled.div`
	font-size: 20px;
	text-align: center;
`

Making composable components that carry their styling with them was just super awesome.

Unfortunately, I hit a wall when I realized that there isn't currently a solution for right-to-left conversion (like CSSJanus or rtl-css-js) and that's a hard requirement for what I'm building. I also had some issues with the size of styled-components at the time (note that you can transpile away a lot of the size if you're willing to give up some dynamic capabilities, which I was unwilling to do).

So after evaluating a bunch of other solutions and trying to enhance existing solutions to be what I wanted them to be, I decided to create my own.

Enter glamorous 💄!

paypal/glamorous

glamorous is React component styling solved with an elegant (inspired) API, small footprint (<5kb gzipped), and great performance (via glamor). It has a very similar API to styled-components and uses similar tools under the hood (glamor). The benefits being:

Let's get a quick look at what a glamorous component looks like:

// Create a <Title> react component that renders an <h1> which is
// centered, palevioletred and sized at 1.5em
const Title = glamorous.h1({
	fontSize: '1.5em',
	textAlign: 'center',
	color: 'palevioletred',
})

// Create a <Wrapper> react component that renders a <section> with
// some padding and a papayawhip background
const Wrapper = glamorous.section({
	padding: '4em',
	background: 'papayawhip',
})

function App() {
	return (
		<Wrapper>
			<Title>Hello World, this is my first glamorous component!</Title>
		</Wrapper>
	)
}

(thanks to styled-components for the example inspiration).

The beauty of glamorous is that all of the cool things you can do with glamor, you can do with glamorous. Here are a few examples:

pseudoclasses

const MyLink = glamorous.a({
	':hover': {
		color: 'red',
	},
})

child-selectors (the escape hatch you should rarely use, but is nice to have)

const MyDiv = glamorous.div({
	display: 'block',
	'& .bold': { fontWeight: 'bold' },
	'& .one': { color: 'blue' },
	':hover .two': { color: 'red' },
})

const ui = (
	<MyDiv>
		<div className="one bold">is blue-bold!</div>
		<div className="two">hover red!</div>
	</MyDiv>
)

media queries

const MyResponsiveDiv = glamorous.div({
	width: '100%',
	padding: 20,
	'[@media](http://twitter.com/media "Twitter profile for @media")(min-width: 400px)':
		{
			width: '85%',
			padding: 0,
		},
})

animations

import { css } from 'glamor' // or require or whatever...

const bounce = css.keyframes({
	'0%': { transform: 'scale(1)', opacity: 0.3 },
	'55%': { transform: 'scale(1.2)', opacity: 1 },
	'100%': { transform: 'scale(1)', opacity: 0.3 },
})

const MyBouncyDiv = glamorous.div({
	animation: `${bounce} 1s infinite`,
	width: 50,
	height: 50,
	backgroundColor: 'red',
})

theming

With the new ThemeProvider (recently added by Alessandro Arnodo), glamorous also supports theming:

const Title = glamorous.h1(
	{
		fontSize: '10px',
	},
	(props, theme) => ({
		color: theme.main.color,
	}),
)

// use <ThemeProvider> to pass theme down the tree
const ui1 = (
	<ThemeProvider theme={theme}>
		<Title>Hello!</Title>
	</ThemeProvider>
)

// it is possible to nest themes
// inner themes will be merged with outers
const ui2 = (
	<ThemeProvider theme={theme}>
		<div>
			<Title>Hello!</Title>
			<ThemeProvider theme={secondaryTheme}>
				{/\* this will be blue */}
				<Title>Hello from here!</Title>
			</ThemeProvider>
		</div>
	</ThemeProvider>
)

And if you need global styles, you can just use glamor to do that (you can do this with styled-components as well). And there many other cool things you can do with glamor (including Server Side Rendering)!

Another great feature of glamorous is it will merge glamor class names together automatically for you. Learn more about that here.


In addition to the styled-components inspired API, glamorous exposes a jsxstyle inspired API. Sometimes, you don't want to give something a name because naming things is hard. Especially with this stuff, you wind up with names like Container and Wrapper and who knows which is which!? So, if you find that something doesn't really need a name, then don't give it one!

const { Div, A } = glamorous

function App() {
	return (
		<Div textAlign="center" color="red">
			<A
				href="[https://brave.com/](https://brave.com)"
				textDecoration="none"
				color="darkorange"
				textShadow="1px 1px 2px orange"
			>
				Browse faster and safer with Brave.
			</A>
			<div>It's fast, fun, and safe!</div>
		</Div>
	)
}

(fun tip: this works too: <glamorous.Div>JSX!!</glamorous.Div>)

Oh, and just for fun, all this excitement around CSS Grid got you salivating? It's trivially supported by glamorous:

// Example inspired by
// [http://gridbyexample.com/examples/example12/](http://gridbyexample.com/examples/example12)
const MyGrid = glamorous.div({
	margin: 'auto',
	backgroundColor: '#fff',
	color: '#444',
	// You can use [@supports](http://twitter.com/supports "Twitter profile for @supports") with glamor!
	// So you can use [@supports](http://twitter.com/supports "Twitter profile for @supports") with glamorous as well!
	'[@supports](http://twitter.com/supports "Twitter profile for @supports") (display: grid)':
		{
			display: 'grid',
			gridGap: 10,
			gridTemplateAreas: `  
      "....... header header"  
      "sidebar content content"  
      "footer  footer  footer"  
    `,
		},
})

const Box = glamorous.div({
	backgroundColor: '#444',
	color: '#fff',
	borderRadius: 5,
	padding: 10,
	fontSize: '150%',
})

const HeaderFooter = glamorous(Box)({
	backgroundColor: '#999',
})

function App() {
	return (
		<MyGrid>
			<HeaderFooter css={{ gridArea: 'header' }}>Header</HeaderFooter>
			<Box css={{ gridArea: 'sidebar' }}>Sidebar</Box>
			<Box css={{ gridArea: 'content' }}>
				Content
				<br />
				More content than we had before so this column is now quite tall.
			</Box>
			<HeaderFooter css={{ gridArea: 'footer' }}>Footer</HeaderFooter>
		</MyGrid>
	)
}

And you get:

Example inspired by http://gridbyexample.com/examples/example12/

I hope you enjoy glamorous 💄 🌟 👀!

See you around on twitter: @glamorousCSS and @kentcdodds

With ❤️ from PayPal (we're hiring!)