Skip to content

Commit 72ec7db

Browse files
authored
[ENG-5498] Tailwind docs (#1286)
* [ENG-5498] Tailwind docs * typo
1 parent 269eb38 commit 72ec7db

File tree

5 files changed

+294
-238
lines changed

5 files changed

+294
-238
lines changed

docs/styling/custom-stylesheets.md

Lines changed: 0 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -63,81 +63,6 @@ app.add_page(
6363
)
6464
```
6565

66-
## Styling with Tailwind
67-
68-
You can integrate custom Tailwind themes within your Reflex app as well. The setup process is similar to the CSS Styling method mentioned above, with only a few minor variations.
69-
70-
Begin by creating a CSS file inside your `assets` folder. Inside the CSS file, include the following Tailwind directives:
71-
72-
```css
73-
@tailwind base;
74-
@tailwind components;
75-
@tailwind utilities;
76-
77-
:root {
78-
--background: blue;
79-
--foreground: green;
80-
}
81-
82-
.dark {
83-
--background: darkblue;
84-
--foreground: lightgreen;
85-
}
86-
```
87-
88-
We define a couple of custom CSS variables (--background and --foreground) that will be used throughout your app for styling. These variables can be dynamically updated based on the theme.
89-
90-
Tailwind defaults to light mode, but to handle dark mode, you can define a separate set of CSS variables under the .dark class.
91-
92-
Tailwind Directives (@tailwind base, @tailwind components, @tailwind utilities): These are essential Tailwind CSS imports that enable the default base styles, components, and utility classes.
93-
94-
Next, you'll need to configure Tailwind in your `rxconfig.py` file to ensure that the Reflex app uses your custom Tailwind setup.
95-
96-
```python
97-
import reflex as rx
98-
99-
tailwind_config = {
100-
"plugins": ["@tailwindcss/typography"],
101-
"theme": {
102-
"extend": {
103-
"colors": {
104-
"background": "var(--background)",
105-
"foreground": "var(--foreground)"
106-
},
107-
}
108-
},
109-
}
110-
111-
config = rx.Config(
112-
app_name="app",
113-
tailwind=tailwind_config,
114-
)
115-
```
116-
117-
In the theme section, we're extending the default Tailwind theme to include custom colors. Specifically, we're referencing the CSS variables (--background and --foreground) that were defined earlier in your CSS file.
118-
119-
The rx.Config object is used to initialize and configure your Reflex app. Here, we're passing the tailwind_config dictionary to ensure Tailwind's custom setup is applied to the app.
120-
121-
Finally, to apply your custom styles and Tailwind configuration, you need to reference the CSS file you created in your `assets` folder inside the `rx.App` setup. This will allow you to use the custom properties (variables) directly within your Tailwind classes.
122-
123-
In your `app.py` (or main application file), make the following changes:
124-
125-
```python
126-
app = rx.App(
127-
theme=rx.theme(appearance="light"),
128-
stylesheets=["/style.css"],
129-
)
130-
app.add_page(
131-
rx.center(
132-
rx.text("Tailwind & Reflex!"),
133-
class_name="bg-background w-full h-[100vh]",
134-
),
135-
"/",
136-
)
137-
```
138-
139-
The bg-background class uses the --background variable (defined in the CSS file), which will be applied as the background color.
140-
14166
## Fonts
14267

14368
You can take advantage of Reflex's support for custom stylesheets to add custom fonts to your app.

docs/styling/overview.md

Lines changed: 0 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -172,64 +172,6 @@ app = rx.App(
172172

173173
Additionally you can modify the theme of your app through using the `Theme Panel` component which can be found in the [Theme Panel docs]({library.other.theme.path}).
174174

175-
176-
177-
178-
## Tailwind
179-
180-
Reflex supports [Tailwind CSS]({"https://tailwindcss.com/"}) out of the box. To enable it, pass in a dictionary for the `tailwind` argument of your `rxconfig.py`:
181-
182-
```python
183-
import reflex as rx
184-
185-
186-
class AppConfig(rx.Config):
187-
pass
188-
189-
190-
config = AppConfig(
191-
app_name="app",
192-
db_url="sqlite:///reflex.db",
193-
env=rx.Env.DEV,
194-
tailwind=\{},
195-
)
196-
```
197-
198-
All Tailwind configuration options are supported. Plugins and presets are automatically wrapped in `require()`:
199-
200-
```python
201-
config = AppConfig(
202-
app_name="app",
203-
db_url="sqlite:///reflex.db",
204-
env=rx.Env.DEV,
205-
tailwind={
206-
"theme": {
207-
"extend": \{},
208-
},
209-
"plugins": ["@tailwindcss/typography"],
210-
},
211-
)
212-
```
213-
214-
You can use any of the [utility classes]({"https://tailwindcss.com/docs/utility-first"}) under the `class_name` prop:
215-
216-
```python demo
217-
rx.box(
218-
"Hello World",
219-
class_name="text-4xl text-center text-blue-500",
220-
)
221-
```
222-
223-
### Disabling Tailwind
224-
225-
If you want to disable Tailwind in your configuration, you can do so by setting the `tailwind` config to `None`. This can be useful if you need to temporarily turn off Tailwind for your project:
226-
227-
```python
228-
config = rx.Config(app_name="app", tailwind=None)
229-
```
230-
231-
With this configuration, Tailwind will be disabled, and no Tailwind styles will be applied to your application.
232-
233175
## Special Styles
234176

235177
We support all of Chakra UI's [pseudo styles]({"https://v2.chakra-ui.com/docs/styled-system/style-props#pseudo"}).

docs/styling/tailwind.md

Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
```python exec
2+
import reflex as rx
3+
from pcweb.pages.docs import library
4+
5+
```
6+
7+
# Tailwind
8+
9+
Reflex supports [Tailwind CSS]({"https://tailwindcss.com/"}) out of the box. To enable it, pass in a dictionary for the `tailwind` argument of your `rxconfig.py`:
10+
11+
```python
12+
import reflex as rx
13+
14+
config = rx.Config(
15+
app_name="myapp",
16+
tailwind=\{},
17+
)
18+
```
19+
20+
All Tailwind configuration options are supported. Plugins and presets are automatically wrapped in `require()`:
21+
22+
```python
23+
config = rx.Config(
24+
app_name="app",
25+
tailwind={
26+
"plugins": ["@tailwindcss/typography"],
27+
},
28+
)
29+
```
30+
31+
You can use any of the [utility classes]({"https://tailwindcss.com/docs/utility-first"}) under the `class_name` prop:
32+
33+
```python demo
34+
rx.box(
35+
"Hello World",
36+
class_name="text-4xl text-center text-blue-500",
37+
)
38+
```
39+
40+
### Disabling Tailwind
41+
42+
If you want to disable Tailwind in your configuration, you can do so by setting the `tailwind` config to `None`. This can be useful if you need to temporarily turn off Tailwind for your project:
43+
44+
```python
45+
config = rx.Config(app_name="app", tailwind=None)
46+
```
47+
48+
With this configuration, Tailwind will be disabled, and no Tailwind styles will be applied to your application.
49+
50+
51+
## Custom theme
52+
53+
You can integrate custom Tailwind themes within your Reflex app as well. The setup process is similar to the CSS Styling method mentioned above, with only a few minor variations.
54+
55+
Begin by creating a CSS file inside your `assets` folder. Inside the CSS file, include the following Tailwind directives:
56+
57+
```css
58+
@tailwind base;
59+
@tailwind components;
60+
@tailwind utilities;
61+
62+
:root {
63+
--background: blue;
64+
--foreground: green;
65+
}
66+
67+
.dark {
68+
--background: darkblue;
69+
--foreground: lightgreen;
70+
}
71+
```
72+
73+
We define a couple of custom CSS variables (--background and --foreground) that will be used throughout your app for styling. These variables can be dynamically updated based on the theme.
74+
75+
Tailwind defaults to light mode, but to handle dark mode, you can define a separate set of CSS variables under the .dark class.
76+
77+
Tailwind Directives (@tailwind base, @tailwind components, @tailwind utilities): These are essential Tailwind CSS imports that enable the default base styles, components, and utility classes.
78+
79+
Next, you'll need to configure Tailwind in your `rxconfig.py` file to ensure that the Reflex app uses your custom Tailwind setup.
80+
81+
```python
82+
import reflex as rx
83+
84+
tailwind_config = {
85+
"plugins": ["@tailwindcss/typography"],
86+
"theme": {
87+
"extend": {
88+
"colors": {
89+
"background": "var(--background)",
90+
"foreground": "var(--foreground)"
91+
},
92+
}
93+
},
94+
}
95+
96+
config = rx.Config(
97+
app_name="app",
98+
tailwind=tailwind_config,
99+
)
100+
```
101+
102+
In the theme section, we're extending the default Tailwind theme to include custom colors. Specifically, we're referencing the CSS variables (--background and --foreground) that were defined earlier in your CSS file.
103+
104+
The rx.Config object is used to initialize and configure your Reflex app. Here, we're passing the tailwind_config dictionary to ensure Tailwind's custom setup is applied to the app.
105+
106+
Finally, to apply your custom styles and Tailwind configuration, you need to reference the CSS file you created in your `assets` folder inside the `rx.App` setup. This will allow you to use the custom properties (variables) directly within your Tailwind classes.
107+
108+
In your `app.py` (or main application file), make the following changes:
109+
110+
```python
111+
app = rx.App(
112+
theme=rx.theme(appearance="light"),
113+
stylesheets=["/style.css"],
114+
)
115+
app.add_page(
116+
rx.center(
117+
rx.text("Tailwind & Reflex!"),
118+
class_name="bg-background w-full h-[100vh]",
119+
),
120+
"/",
121+
)
122+
```
123+
124+
The bg-background class uses the --background variable (defined in the CSS file), which will be applied as the background color.
125+
126+
## Dynamic Styling
127+
128+
You can style a component based of a condition using `rx.cond` or `rx.match`.
129+
130+
```python demo exec
131+
class TailwindState(rx.State):
132+
active = False
133+
134+
@rx.event
135+
def toggle_active(self):
136+
self.active = not self.active
137+
138+
def tailwind_demo():
139+
return rx.el.div(
140+
rx.el.button(
141+
"Click me",
142+
on_click=TailwindState.toggle_active,
143+
class_name=(
144+
"px-4 py-2 text-white rounded-md",
145+
rx.cond(
146+
TailwindState.active,
147+
"bg-red-500",
148+
"bg-blue-500",
149+
),
150+
),
151+
),
152+
)
153+
```
154+
155+
## Using Tailwind Classes from the State
156+
157+
When using Tailwind with Reflex, it's important to understand that class names must be statically defined in your code for Tailwind to properly compile them. If you dynamically generate class names from state variables or functions at runtime, Tailwind won't be able to detect these classes during the build process, resulting in missing styles in your application.
158+
159+
For example, this won't work correctly because the class names are defined in the state:
160+
161+
```python demo exec
162+
class TailwindState(rx.State):
163+
active = False
164+
165+
@rx.var
166+
def button_class(self) -> str:
167+
return "bg-accent" if self.active else "bg-secondary"
168+
169+
@rx.event
170+
def toggle_active(self):
171+
self.active = not self.active
172+
173+
def tailwind_demo():
174+
return rx.el.button(
175+
f"Click me: {TailwindState.active}",
176+
class_name=TailwindState.button_class,
177+
on_click=TailwindState.toggle_active,
178+
)
179+
```
180+
181+
## Using Tailwind with Reflex Core Components
182+
183+
Reflex core components are built on Radix Themes, which means they come with pre-defined styling. When you apply Tailwind classes to these components, you may encounter styling conflicts or unexpected behavior as the Tailwind styles compete with the built-in Radix styles.
184+
185+
For the best experience when using Tailwind CSS in your Reflex application, we recommend using the lower-level `rx.el` components. These components don't have pre-applied styles, giving you complete control over styling with Tailwind classes without any conflicts. Check the list of HTML components [here]({library.other.html.path}).
186+

pcweb/components/docpage/sidebar/sidebar_items/learn.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ def get_sidebar_items_frontend():
7777
styling.custom_stylesheets,
7878
styling.layout,
7979
styling.common_props,
80+
styling.tailwind,
8081
],
8182
),
8283
create_item(

0 commit comments

Comments
 (0)