# `tw` Prop
The `tw` prop is a convenient shorthand for `className={tw(...)}`. Here's an example, shamelessly borrowed from the TailwindCSS docs 😏
```js
const Notification = ({ title, message }) => (
{title}
{message}
)
// compiles to:
import { tw } from 'twind'
const Notification = ({ title, message }) => (
{title}
{message}
)
```
Also works nicely with [`css`](https://twind.dev/docs/modules/twind_css.html), [`apply`](https://twind.dev/docs/modules/twind.html#apply-function), and [whatever you can pass to `tw()`](https://twind.dev/docs/modules/twind.html#tw-function)
```js
import { apply } from 'twind'
import { css } from 'twind/css'
const buttonStyle = apply`
block p-3 w-full
font-medium text-left leading-none
transition
ring(2 inset transparent)
hover:(bg-green-100 text-green-900)
focus:ring-green-500
`
const Example = () => (
)
```
## Creating components
For single element styles, consider making partial styles with [`apply`](https://twind.dev/docs/modules/twind.html#apply-function) and reusing them:
```ts
// components.ts
import { apply } from 'twind'
export const buttonStyle = apply`text-white rounded shadow p-3 bg-blue-600 hover:bg-blue-700`
```
```tsx
// App.tsx
import { buttonStyle } from './components'
export default function App() {
return (
Call to action!
)
}
```
For more complex cases, our recommendation is to define explicit props for customizability. This avoids various caveats around css class ordering, precedence, implementation details, etc.
```tsx
import { apply } from 'twind'
import type { ReactNode } from 'react'
type Props = {
size?: 'medium' | 'large'
color?: 'red' | 'blue'
isLoading?: boolean
children?: ReactNode
}
export default function Button({ size = 'medium', color = 'blue', isLoading, children }: Props) {
const buttonStyle = apply`
text-white rounded shadow
${size === 'medium' && `p-3`}
${size === 'large' && `p-4`}
${color === 'blue' && `bg-blue-600 hover:bg-blue-700`}
${color === 'red' && `bg-red-600 hover:bg-red-700`}
`
return (
)
}
```
## `tw` overrides
One might want to create components where the style can be overridden via the `tw` prop. Here's how to do that:
```tsx
import type { ReactNode, ComponentPropsWithoutRef } from 'react'
import { apply } from 'twind'
type Props = ComponentPropsWithoutRef<'button'>
export default function Button({ className, ...props }: Props) {
return (
)
}
```
```tsx
import { css } from 'twind'
import Button from './Button'
const example = (
<>
>
)
```
A few things to note:
- `className={className}` is needed here, so that the transformer can see that prop, and merge it with the class generated from the `tw` prop. Without it, if we passed a `className` prop to `Button`, it could override the button's generated class.
```js
// with `className={className}`, it compiles this:
// without, it compiles this:
```
- In less simple cases, this has the potential to break implementation details of the component. e.g. the component declares a `grid` class internally, then receives a `flex` override, which messes up the layout. But for simple single-element cases like this, it's probably fine. ¯\\\_(ツ)\_/¯