tailwind_theme_picker 0.1.0 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 3da3ca739dd4a28d67ce9e6077556e1c124df7a5626039126e4c842f903bb102
|
|
4
|
+
data.tar.gz: 89fd5389cb75b14868cab742c2d66412fc6a90edbf506e489c3e9ada2fba1ad2
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: d87acbb91bdc0f9aa40235478c10e4819cad44403440ab495ce331685a9e79e2f60767503b1d6905b2621aca8d80d3fb5fcf79393038ec180dabfda93c6a81f9
|
|
7
|
+
data.tar.gz: c198c881ae22f8d1691dec78cb3fca9fc1c0c15f0c2ea0db072468f1845d49716d55f07625e443b1525eb3d0e4da6b77fd14a77063aa7d39dc291e831e83e623
|
data/README.md
CHANGED
|
@@ -14,7 +14,7 @@ Drop-in theme + light/dark picker for Tailwind-based Rails apps.
|
|
|
14
14
|
|
|
15
15
|
```ruby
|
|
16
16
|
# Gemfile
|
|
17
|
-
gem "tailwind_theme_picker"
|
|
17
|
+
gem "tailwind_theme_picker"
|
|
18
18
|
```
|
|
19
19
|
|
|
20
20
|
```bash
|
|
@@ -38,12 +38,12 @@ end
|
|
|
38
38
|
### Wire up the layout
|
|
39
39
|
|
|
40
40
|
```slim
|
|
41
|
-
html lang="en" *
|
|
41
|
+
html lang="en" *tailwind_theme_picker_html_attrs
|
|
42
42
|
head
|
|
43
43
|
/ ...stylesheets, importmap...
|
|
44
|
-
=
|
|
44
|
+
= tailwind_theme_picker_fouc_script
|
|
45
45
|
body
|
|
46
|
-
=
|
|
46
|
+
= render_tailwind_theme_picker
|
|
47
47
|
= yield
|
|
48
48
|
```
|
|
49
49
|
|
|
@@ -69,8 +69,8 @@ If you previously had `app/javascript/controllers/theme_controller.js`, delete i
|
|
|
69
69
|
## How persistence works
|
|
70
70
|
|
|
71
71
|
1. Stimulus controller writes both cookies on every change.
|
|
72
|
-
2. On the next request, `
|
|
73
|
-
3. On the user's *first* visit (no cookies), `
|
|
72
|
+
2. On the next request, `tailwind_theme_picker_html_attrs` reads them and paints `<html>` server-side. No FOUC.
|
|
73
|
+
3. On the user's *first* visit (no cookies), `tailwind_theme_picker_fouc_script` emits ~250 bytes of inline JS that paints from localStorage or system preference. After that, cookies take over and the helper returns an empty string.
|
|
74
74
|
|
|
75
75
|
## Configuration reference
|
|
76
76
|
|
|
@@ -3,7 +3,7 @@ module TailwindThemePicker
|
|
|
3
3
|
module ViewHelper
|
|
4
4
|
|
|
5
5
|
# Emoji defaults so the gem has no icon-library dependency. Override by
|
|
6
|
-
# passing :icons to
|
|
6
|
+
# passing :icons to render_tailwind_theme_picker.
|
|
7
7
|
DEFAULT_ICONS = {
|
|
8
8
|
palette: "🎨",
|
|
9
9
|
times: "❌",
|
|
@@ -12,8 +12,8 @@ module TailwindThemePicker
|
|
|
12
12
|
}.freeze
|
|
13
13
|
|
|
14
14
|
# Returns HTML attributes for the <html> tag based on the request's cookies.
|
|
15
|
-
# Use like: html lang="en" *
|
|
16
|
-
def
|
|
15
|
+
# Use like: html lang="en" *tailwind_theme_picker_html_attrs
|
|
16
|
+
def tailwind_theme_picker_html_attrs
|
|
17
17
|
config = TailwindThemePicker.configuration
|
|
18
18
|
theme = cookies[config.theme_cookie].presence
|
|
19
19
|
theme = config.default unless config.themes.include?(theme)
|
|
@@ -25,7 +25,7 @@ module TailwindThemePicker
|
|
|
25
25
|
|
|
26
26
|
# Whether the current request already has theme cookies set. Useful for
|
|
27
27
|
# skipping the FOUC fallback script.
|
|
28
|
-
def
|
|
28
|
+
def tailwind_theme_picker_cookies_present?
|
|
29
29
|
config = TailwindThemePicker.configuration
|
|
30
30
|
cookies[config.theme_cookie].present? && cookies[config.mode_cookie].present?
|
|
31
31
|
end
|
|
@@ -33,8 +33,8 @@ module TailwindThemePicker
|
|
|
33
33
|
# Inline <script> that paints theme + mode classes from cookies/localStorage
|
|
34
34
|
# before the rest of the page renders. Only needed on the first visit (when
|
|
35
35
|
# the cookies aren't yet set server-side) — pass force: true to always emit.
|
|
36
|
-
def
|
|
37
|
-
return "".html_safe if !force &&
|
|
36
|
+
def tailwind_theme_picker_fouc_script(force: false)
|
|
37
|
+
return "".html_safe if !force && tailwind_theme_picker_cookies_present?
|
|
38
38
|
|
|
39
39
|
config = TailwindThemePicker.configuration
|
|
40
40
|
nonce = (respond_to?(:content_security_policy_nonce) ? content_security_policy_nonce : nil)
|
|
@@ -53,7 +53,7 @@ module TailwindThemePicker
|
|
|
53
53
|
content_tag(:script, js.html_safe, nonce: nonce)
|
|
54
54
|
end
|
|
55
55
|
|
|
56
|
-
def
|
|
56
|
+
def render_tailwind_theme_picker(themes: nil, default: nil, icons: {})
|
|
57
57
|
config = TailwindThemePicker.configuration
|
|
58
58
|
render(
|
|
59
59
|
partial: "tailwind_theme_picker/picker",
|
|
@@ -38,9 +38,9 @@ module TailwindThemePicker
|
|
|
38
38
|
say " 1. Add to your Tailwind input CSS (e.g. app/assets/tailwind/application.css):"
|
|
39
39
|
say " @import './tailwind_theme_picker/themes';", :cyan
|
|
40
40
|
say " 2. Wire up your layout <html> tag and body:"
|
|
41
|
-
say " html lang=\"en\" *
|
|
42
|
-
say " =
|
|
43
|
-
say " =
|
|
41
|
+
say " html lang=\"en\" *tailwind_theme_picker_html_attrs", :cyan
|
|
42
|
+
say " = tailwind_theme_picker_fouc_script # in <head>"
|
|
43
|
+
say " = render_tailwind_theme_picker # in <body>"
|
|
44
44
|
say ""
|
|
45
45
|
say "After upgrading the gem, re-run this generator to pick up any new themes."
|
|
46
46
|
end
|