Skip to content

Commit 9d7a6f9

Browse files
authored
Merge pull request #5 from lincolnloop/make-css-customizable-per-project
Make Pattern Library CSS Customizable
2 parents f363c99 + b967a34 commit 9d7a6f9

File tree

9 files changed

+80
-14
lines changed

9 files changed

+80
-14
lines changed

pattern_library/__init__.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@
3535
("templates", ["patterns/templates"]),
3636
("pages", ["patterns/pages"]),
3737
),
38+
# CUSTOM_CSS allows users to override pattern library styles by providing a path to a CSS file
39+
# (relative to STATIC_URL) that contains CSS custom properties. This file will be included
40+
# after the main bundle to override default styles.
41+
# Example: "css/pattern-library-custom.css"
42+
"CUSTOM_CSS": None,
43+
# SITE_TITLE allows users to customize the pattern library title displayed in the header
44+
"SITE_TITLE": "Django Pattern Library",
3845
}
3946

4047

pattern_library/static/pattern_library/src/scss/_config.scss

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
1-
// Overwrite the title, main colour and font family here
2-
$site-title: 'Django Pattern Library';
3-
$color-primary: #34b2b2;
4-
$family-primary: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen-Sans, Ubuntu, Cantarell, 'Helvetica Neue', sans-serif;
1+
// Configuration with support for runtime customization via CSS custom properties
2+
3+
// Default SCSS variables for compile-time usage (needed for Sass functions)
4+
$color-primary: #34b2b2 !default;
5+
$family-primary: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen-Sans, Ubuntu, Cantarell, 'Helvetica Neue', sans-serif !default;
6+
7+
// CSS Custom Properties for runtime customization
8+
html, :root {
9+
--color-primary: #{$color-primary};
10+
--family-primary: #{$family-primary};
11+
}
512

613
// $color--primary + $family--primary are in _config.scss
714
$white: #fff;

pattern_library/static/pattern_library/src/scss/abstracts/_base.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
html,
1010
body {
1111
margin: 0;
12-
font-family: $family-primary;
12+
font-family: var(--family-primary);
1313
height: 100%;
1414
}
1515

pattern_library/static/pattern_library/src/scss/components/_list.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070
&:hover,
7171
&.is-active {
7272
background: $white;
73-
border-left: 5px solid $color-primary;
73+
border-left: 5px solid var(--color-primary);
7474
}
7575
}
7676
}

pattern_library/static/pattern_library/src/scss/components/_tabbed-listing.scss

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@
2525

2626
&:hover {
2727
color: #000;
28-
background-color: $color-primary;
28+
background-color: var(--color-primary);
2929
}
3030
}
3131

3232
&--active {
3333
> a {
3434
color: #000;
35-
background-color: $color-primary;
35+
background-color: var(--color-primary);
3636
}
3737
}
3838

pattern_library/static/pattern_library/src/scss/layout/_header.scss

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
@use "../config" as *;
22

33
.header {
4-
background-color: $color-primary;
4+
background-color: var(--color-primary);
55
display: flex;
66
align-items: center;
77
height: 45px;
@@ -17,10 +17,8 @@
1717
letter-spacing: 1px;
1818
font-size: 16px;
1919
margin-left: 15px;
20-
21-
&::after {
22-
content: $site-title;
23-
}
20+
margin-top: 0;
21+
margin-bottom: 0;
2422

2523
@media only screen and (min-width: 600px) {
2624
font-size: 22px;

pattern_library/templates/pattern_library/base.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{% load static %}
2+
{% load pattern_library_tags %}
23

34
<!DOCTYPE html>
45
<html lang="en">
@@ -9,6 +10,7 @@
910
<meta name="viewport" content="width=device-width, initial-scale=1" />
1011
<link rel="icon" href="data:image/svg+xml,<svg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%220 0 100 100%22><text y=%22.9em%22 font-size=%2290%22>♻️</text></svg>"/>
1112
<script type="text/javascript" src="{% static 'pattern_library/dist/bundle.js' %}"></script>
13+
{% pattern_library_custom_css %}
1214
</head>
1315

1416
<body class="body">
@@ -19,7 +21,7 @@
1921
</a>
2022
<h1 class="header__title">
2123
<span class="sr-only">Pattern Library</span>
22-
{# Set in _config.scss #}
24+
{% pattern_library_site_title %}
2325
</h1>
2426
</header>
2527
<aside class="sidebar">
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Template tags for django-pattern-library
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
from django import template
2+
from django.templatetags.static import static
3+
from django.utils.safestring import mark_safe
4+
5+
from pattern_library import get_setting
6+
7+
register = template.Library()
8+
9+
10+
@register.simple_tag
11+
def pattern_library_custom_css():
12+
"""
13+
Include custom CSS for pattern library customization.
14+
15+
This tag optionally includes an external CSS file for additional customization.
16+
17+
Usage in templates:
18+
{% load pattern_library_tags %}
19+
{% pattern_library_custom_css %}
20+
21+
Example PATTERN_LIBRARY setting:
22+
PATTERN_LIBRARY = {
23+
"CUSTOM_CSS": "css/pattern-library-custom.css" # relative to STATIC_URL
24+
}
25+
"""
26+
css_content = []
27+
28+
# Include external CSS file if specified
29+
custom_css_path = get_setting("CUSTOM_CSS")
30+
if custom_css_path:
31+
try:
32+
css_url = static(custom_css_path)
33+
css_content.append(
34+
f'<link rel="stylesheet" type="text/css" href="{css_url}">'
35+
)
36+
except Exception:
37+
pass # If static file handling fails, just skip the external file
38+
39+
return mark_safe("\n".join(css_content))
40+
41+
42+
@register.simple_tag
43+
def pattern_library_site_title():
44+
"""
45+
Get the site title for the pattern library.
46+
47+
Usage in templates:
48+
{% load pattern_library_tags %}
49+
{% pattern_library_site_title %}
50+
"""
51+
return get_setting("SITE_TITLE")

0 commit comments

Comments
 (0)