Skip to content

Commit e7c82fc

Browse files
add roadmap examples (#18)
1 parent e8832c5 commit e7c82fc

File tree

1 file changed

+207
-8
lines changed

1 file changed

+207
-8
lines changed

README.md

Lines changed: 207 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,23 +73,23 @@ In `button.html`, create a simple HTML button. Use `{{ slot }}` to indicate wher
7373
```htmldjango
7474
{# templates/bird/button.html #}
7575
<button>
76-
{{ slot }}
76+
{{ slot }}
7777
</button>
7878
```
7979

8080
To use your component in a Django template, use the `{% bird %}` templatetag. The content between `{% bird %}` and `{% endbird %}` becomes the `{{ slot }}` content.
8181

8282
```htmldjango
8383
{% bird button %}
84-
Click me!
84+
Click me!
8585
{% endbird %}
8686
```
8787

8888
django-bird automatically recognizes components in the bird directory, so no manual registration is needed. When Django processes the template, django-bird replaces the `{% bird %}` tag with the component's HTML, inserting the provided content into the slot, resulting in:
8989
9090
```html
9191
<button>
92-
Click me!
92+
Click me!
9393
</button>
9494
```
9595
@@ -110,13 +110,118 @@ For a full overview of the features and configuration options, please refer to t
110110
111111
## Roadmap
112112
113-
Here's a roadmap of features I'm considering for django-bird:
113+
Below are a bunch of features I'd like to bring to django-bird.
114+
115+
I have included code snippets where applicable, but they are back-of-the-napkin sketches of potential APIs -- subject to change if and when the feature is actually introduced.
114116

115117
### Static Asset Collection
116118

117-
This is table stakes for a modern Django template component library. You should be able to define CSS and JS for a component and have it loaded automatically when you use that component.
119+
This is table stakes for a modern Django template component library. The goal is to allow you to define CSS and JS for a component and have it loaded automatically when you use that component.
120+
121+
Unlike django-components, which uses the Django forms library pattern with a `class Media` declaration, the idea is to allow defining styles and scripts within a single component file, or adjacent to a component. django-bird would then collect and compile these assets, streamlining the whole process.
122+
123+
Here's a potential example of how you might define a button component with inline styles and scripts:
124+
125+
```htmldjango
126+
{# templates/bird/button.html #}
127+
<button>
128+
{{ slot }}
129+
</button>
130+
<style>
131+
button {
132+
background-color: red;
133+
padding: 10px 20px;
134+
color: white;
135+
border: none;
136+
cursor: pointer;
137+
}
138+
</style>
139+
<script>
140+
$bird.addEventListener('click', () => {
141+
alert('This specific button was clicked!');
142+
});
143+
</script>
144+
```
145+
146+
The `$bird` variable in the JavaScript is a potential special identifier that could be used to scope the script to the specific component instance.
147+
148+
Alternatively, you could potentially separate the styles and scripts into their own files:
149+
150+
```htmldjango
151+
{# templates/bird/button.html #}
152+
<button>
153+
{{ slot }}
154+
</button>
155+
```
156+
157+
```css
158+
/* templates/bird/button.css */
159+
button {
160+
background-color: red;
161+
padding: 10px 20px;
162+
color: white;
163+
border: none;
164+
cursor: pointer;
165+
}
166+
```
167+
168+
```javascript
169+
// templates/bird/button.js
170+
$bird.addEventListener('click', () => {
171+
alert('This specific button was clicked!');
172+
});
173+
```
174+
175+
To use this component and include its assets in your template, the API might look something like this:
176+
177+
```htmldjango
178+
<html>
179+
<head>
180+
{% django_bird_css %}
181+
</head>
182+
<body>
183+
{% bird button %}
184+
Click me
185+
{% endbird %}
186+
187+
{% django_bird_js %}
188+
</body>
189+
</html>
190+
```
191+
192+
In this conceptual setup, `{% django_bird_css %}` and `{% django_bird_js %}` would automatically include the collected and compiled CSS and JavaScript for all components used in the template.
118193
119-
Unlike django-components, which uses the Django forms library pattern with a `class Media` declaration, I want to allow defining styles and scripts within a single component file. django-bird will then collect and compile these assets, streamlining the whole process.
194+
To give you an idea of what the final compiled output might look like, here's a hypothetical example of the HTML that could be generated:
195+
196+
```htmldjango
197+
<html>
198+
<head>
199+
<style>
200+
[data-bird-id="button-1"] {
201+
background-color: red;
202+
padding: 10px 20px;
203+
color: white;
204+
border: none;
205+
cursor: pointer;
206+
}
207+
</style>
208+
</head>
209+
<body>
210+
<button data-bird-id="button-1">
211+
Click me
212+
</button>
213+
214+
<script>
215+
(function() {
216+
const $bird = document.querySelector('[data-bird-id="button-1"]');
217+
$bird.addEventListener('click', () => {
218+
alert('This specific button was clicked!');
219+
});
220+
})();
221+
</script>
222+
</body>
223+
</html>
224+
```
120225

121226
### Component Islands
122227

@@ -126,24 +231,118 @@ If you're new to component islands, think of it as fancy lazy-loading. You can s
126231

127232
There's a neat library from the 11ty team called is-land that could work well here. I'll probably start by integrating it directly, but after looking at their source code, I might end up bringing it in and customizing it for django-bird's specific needs.
128233
234+
Here's how the API for component islands might look in django-bird:
235+
236+
```htmldjango
237+
{% bird button on="load" %}
238+
This button loads... on load
239+
{% endbird %}
240+
241+
{% bird button on="idle" %}
242+
This button loads after page load
243+
{% endbird %}
244+
245+
{% bird button on="visible" %}
246+
This button loads when scrolled into view
247+
{% endbird %}
248+
```
249+
250+
In this example, we're using an `on` attribute to specify when each button's JavaScript should be loaded and executed. This approach could significantly improve page load times and performance, especially for pages with many interactive components.
251+
129252
### Custom HTML Tag
130253

131254
I'm a huge fan of the approaches taken by libraries like [django-cotton](https://github.com/wrabit/django-cotton), [dj-angles](https://github.com/adamghill/dj-angles), and Laravel's [Flux](https://fluxui.dev). They let you use custom HTML-like elements that compile down to native templatetags during loading.
132255

133256
This gives you the full power of Django's template language, but in a much nicer package. Compare [this django-allauth template](https://github.com/pennersr/django-allauth/blob/f03ff4dd48e5b1680a57dca56617bf94c928f2cf/allauth/templates/account/email.html) with [these django-cotton examples](https://github.com/wrabit/django-cotton#walkthrough). The allauth template, while powerful, is a mess of tags that barely resembles HTML. The cotton templates, on the other hand, look like clean, custom web elements.
134257
135-
After working with devs from the JavaScript world and using a handful of JavaScript frameworks myself, Django templates can feel ancient compared to JSX. A custom HTML tag approach won't solve all these issues, but it's a step in the right direction.
258+
After working with devs from the JavaScript world and using a handful of JavaScript frameworks myself, Django templates can feel ancient compared to JSX. A custom HTML tag approach could offer a more familiar and readable syntax for component-based development in Django, bridging the gap between traditional Django templates and modern frontend practices.
259+
260+
Here's a comparison of how a button component might be used with the current django-bird syntax and a potential custom HTML tag syntax:
261+
262+
```htmldjango
263+
{% bird button %}
264+
Click me
265+
{% endbird %}
266+
267+
<bird:button>
268+
Click me
269+
</bird:button>
270+
```
136271

137272
### Scoped CSS Styles
138273

139274
I love how Svelte and other JS frameworks let you use a simple `<style>` tag with broad selectors (`p` instead of `.card-body`, `button` instead of `.submit-btn`), then scope those styles to just that component. While I'm a Tailwind CSS fan, having this escape hatch for quick style tweaks would be fantastic.
140275
276+
Here's how a component with scoped styles might look in django-bird:
277+
278+
```htmldjango
279+
{# templates/bird/button.html #}
280+
<button>
281+
{{ slot }}
282+
</button>
283+
<style>
284+
button {
285+
background-color: red;
286+
}
287+
</style>
288+
```
289+
290+
You would use this component in your template like this:
291+
292+
```htmldjango
293+
<html>
294+
<head>
295+
{% django_bird_css %}
296+
</head>
297+
<body>
298+
{% bird button %}
299+
Click me
300+
{% endbird %}
301+
</body>
302+
</html>
303+
```
304+
305+
And here's a potential example of how django-bird might compile this to ensure the styles are scoped to just this component:
306+
307+
```htmldjango
308+
<html>
309+
<head>
310+
<style>
311+
#bird-12fdsa33 {
312+
button {
313+
background-color: red;
314+
}
315+
}
316+
</style>
317+
</head>
318+
<body>
319+
<button id="bird-12fdsa33">
320+
Click me
321+
</button>
322+
</body>
323+
</html>
324+
```
325+
141326
### Integration with Tailwind CSS
142327
143-
Hot 🔥 take: ditch most of Tailwind's atomic classes and write your styles in a CSS file (shocking, I know!), but process it with Tailwind. This gives you modern CSS power without the atomic class juggling, plus you still get to use Tailwind's awesome design system -- which in my mind is _the_ reason to use Tailwind CSS. I could take or leave the atomic styles, but that design system I cannot develop without.
328+
Hot 🔥 take: if you're using Tailwind, you should ditch most of Tailwind's atomic classes and write your styles in a CSS file (shocking, I know!), but process it with Tailwind. This gives you modern CSS power without the atomic class juggling, plus you still get to use Tailwind's awesome design system -- which in my mind is _the_ reason to use Tailwind CSS. I could take or leave the atomic styles, but that design system I cannot develop without.
144329

145330
I'd love for django-bird components to support this workflow, letting you write clean, Tailwind-processed styles right in your components.
146331
332+
Here's how a button component using Tailwind's design system might look in django-bird:
333+
334+
```htmldjango
335+
{# templates/bird/button.html #}
336+
<button>
337+
{{ slot }}
338+
</button>
339+
<style>
340+
button {
341+
background-color: theme("colors.red.500");
342+
}
343+
</style>
344+
```
345+
147346
## License
148347
149348
`django-bird` is licensed under the MIT license. See the [`LICENSE`](LICENSE) file for more information.

0 commit comments

Comments
 (0)