simple_form_tailwind_css 0.0.2 → 1.0.0
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 +4 -4
- data/README.md +99 -8
- data/lib/simple_form/tailwind/error_notification.rb +13 -4
- data/lib/simple_form/tailwind/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8d0537cd908d4ed47ec04dace25a9debe8ddf599170ba4b2be4491315e04c876
|
4
|
+
data.tar.gz: 81f849c320766f54aa4200c4a00e4832009b6b55886ea4d7095ea426994804fa
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ef63e38f5e7d20291826a610bc3e6dc1726055a3ec02f85d612a70d1abdd991c84d65aca1e3c9ecbfe25954ffaef14bb97b71b64da5f719f12422770fb9846c1
|
7
|
+
data.tar.gz: 504d5255785aae42c85ef1059dd4ef4de558715682320a4d66ad8eb72ce938b15ae22bed3cc880b2c8e0cd59dcea173238333f2fede24d43890311f9993ee5eb
|
data/README.md
CHANGED
@@ -2,14 +2,28 @@
|
|
2
2
|
|
3
3
|
Tailwind components for [Simple Form][]
|
4
4
|
|
5
|
-
##
|
5
|
+
## Prerequisites
|
6
|
+
|
7
|
+
You should have these installed first:
|
6
8
|
|
7
|
-
|
9
|
+
* [Simple Form gem][Simple Form]
|
10
|
+
* [Tailwind][]
|
11
|
+
* We're agnostic about install method; the Rails [tailwindcss-rails gem][] is one method
|
12
|
+
* [Heroicon gem][]
|
13
|
+
* Be sure to [run the install generator][heroicon generator] if it's a new dependency to your project
|
8
14
|
|
9
|
-
|
15
|
+
[Simple Form]: https://github.com/heartcombo/simple_form
|
16
|
+
[Tailwind]: https://tailwindcss.com/
|
17
|
+
[tailwindcss-rails gem]: https://github.com/rails/tailwindcss-rails
|
18
|
+
[Heroicon gem]: https://github.com/bharget/heroicon
|
19
|
+
[heroicon generator]: https://github.com/bharget/heroicon#installation
|
20
|
+
|
21
|
+
## Installation
|
22
|
+
|
23
|
+
Add to your application's Gemfile:
|
10
24
|
|
11
25
|
```ruby
|
12
|
-
gem "
|
26
|
+
gem "simple_form_tailwind_css"
|
13
27
|
```
|
14
28
|
|
15
29
|
And then execute:
|
@@ -18,12 +32,33 @@ And then execute:
|
|
18
32
|
$ bundle install
|
19
33
|
```
|
20
34
|
|
21
|
-
|
35
|
+
Next, overwrite your Simple Form initializer with ours:
|
22
36
|
|
23
37
|
```
|
24
38
|
$ rails g simple_form:tailwind:install
|
25
39
|
```
|
26
40
|
|
41
|
+
Finally, for Tailwind 3 we need to modify `tailwind.config.js` to add an environment
|
42
|
+
variable that tells Tailwind where the gem's Ruby source is so that classes used by
|
43
|
+
the gem aren't pruned by Tailwind's JIT:
|
44
|
+
|
45
|
+
```diff
|
46
|
+
--- a/tailwind.config.js
|
47
|
+
+++ b/tailwind.config.js
|
48
|
+
@@ -4,6 +4,7 @@ const colors = require('tailwindcss/colors')
|
49
|
+
/** @type {import('tailwindcss').Config} */
|
50
|
+
module.exports = {
|
51
|
+
content: [
|
52
|
+
+ `${process.env.SIMPLE_FORM_TAILWIND_DIR}/**/*.rb`,
|
53
|
+
'./app/views/**/*.{html,erb,haml}',
|
54
|
+
'./app/helpers/**/*.rb',
|
55
|
+
'./app/javascript/**/*.{js,jsx,ts,tsx,vue}',
|
56
|
+
```
|
57
|
+
|
58
|
+
We're not done yet - jump down to the
|
59
|
+
[Tailwind JIT configuration](#tailwind-jit-configuration) section to read
|
60
|
+
approaches on how to pass in this variable to complete setup.
|
61
|
+
|
27
62
|
## Usage
|
28
63
|
|
29
64
|
Here's an example form demonstrating usage:
|
@@ -89,16 +124,73 @@ Simple Form's error notification is supported, defaulting to a red color with x-
|
|
89
124
|
|
90
125
|

|
91
126
|
|
92
|
-
You can customize the
|
127
|
+
You can customize the icon and Tailwind classes used:
|
93
128
|
|
94
129
|
```erb
|
95
|
-
<%= f.error_notification
|
130
|
+
<%= f.error_notification icon: "information-circle", icon_classes: "h-5 w-5 text-blue-400", message_classes: "text-sm text-blue-700", border_classes: "bg-blue-50 border-l-4 border-blue-400 p-4" %>
|
96
131
|
```
|
97
132
|
|
98
133
|

|
99
134
|
|
100
135
|
The message and other parameters can be customized using the [expected Simple Form configuration options](https://www.rubydoc.info/github/plataformatec/simple_form/SimpleForm%2FFormBuilder:error_notification).
|
101
136
|
|
137
|
+
## Tailwind JIT configuration
|
138
|
+
|
139
|
+
How to pass the `SIMPLE_FORM_TAILWIND_DIR` environment variable in to
|
140
|
+
`tailwind.config.js` is going to vary depending on how your build setup calls
|
141
|
+
the `tailwindcss` CLI.
|
142
|
+
|
143
|
+
If you're using the [tailwindcss-rails gem][] with its `tailwindcss:build` and `tailwindcss:watch` rake tasks, you can override them with something like this:
|
144
|
+
|
145
|
+
```ruby
|
146
|
+
# lib/tasks/tailwindcss.rake
|
147
|
+
|
148
|
+
TAILWIND_COMPILE_COMMAND = "#{RbConfig.ruby} #{Pathname.new(__dir__).to_s}/../../exe/tailwindcss -i '#{Rails.root.join("app/assets/stylesheets/application.tailwind.css")}' -o '#{Rails.root.join("app/assets/builds/tailwind.css")}' -c '#{Rails.root.join("config/tailwind.config.js")}' --minify"
|
149
|
+
SIMPLE_FORM_TAILWIND_GEMDIR = `bundle show simple_form_tailwind_css`
|
150
|
+
|
151
|
+
Rake::Task["tailwindcss:build"].clear
|
152
|
+
Rake::Task["tailwindcss:watch"].clear
|
153
|
+
namespace :tailwindcss do
|
154
|
+
desc "Build your Tailwind CSS"
|
155
|
+
task :build do
|
156
|
+
system({"SIMPLE_FORM_TAILWIND_GEMDIR" => SIMPLE_FORM_TAILWIND_GEMDIR}, TAILWIND_COMPILE_COMMAND, exception: true)
|
157
|
+
end
|
158
|
+
|
159
|
+
desc "Watch and build your Tailwind CSS on file changes"
|
160
|
+
task :watch do
|
161
|
+
system({"SIMPLE_FORM_TAILWIND_GEMDIR" => SIMPLE_FORM_TAILWIND_GEMDIR}, "#{TAILWIND_COMPILE_COMMAND} -w")
|
162
|
+
end
|
163
|
+
end
|
164
|
+
```
|
165
|
+
|
166
|
+
For myself, using [Propshaft][] and an npm script, I made this change:
|
167
|
+
|
168
|
+
```diff
|
169
|
+
--- a/package.json
|
170
|
+
+++ b/package.json
|
171
|
+
@@ -50,10 +50,10 @@
|
172
|
+
},
|
173
|
+
"scripts": {
|
174
|
+
"build-dev:js": "esbuild `find app/javascript -type f` --tsconfig=./tsconfig.json --bundle --sourcemap --outdir=app/assets/builds --public-path=assets",
|
175
|
+
- "build-dev:tailwind": "tailwindcss -i ./app/assets/stylesheets/application.tailwind.css -o ./app/assets/builds/tailwind.css --minify",
|
176
|
+
+ "build-dev:tailwind": "SIMPLE_FORM_TAILWIND_DIR=\"`bundle show simple_form_tailwind_css`\" tailwindcss -i ./app/assets/stylesheets/application.tailwind.css -o ./app/assets/builds/tailwind.css --minify",
|
177
|
+
"build-dev:css": "sass ./app/assets/stylesheets/application.sass.scss:./app/assets/builds/application.css --no-source-map --load-path=node_modules"
|
178
|
+
}
|
179
|
+
}
|
180
|
+
```
|
181
|
+
|
182
|
+
[Propshaft]: https://github.com/rails/propshaft
|
183
|
+
|
184
|
+
As an absolute last resort, instead of dynamically setting the `content` config
|
185
|
+
option you could use `safelist` instead. [Here's an example][safelist example]
|
186
|
+
from the project issues.
|
187
|
+
|
188
|
+
However, this approach is [discouraged by Tailwind][], and is unsupported by this
|
189
|
+
gem as it's likely to break in the future if class names are changed.
|
190
|
+
|
191
|
+
[discouraged by Tailwind]: https://tailwindcss.com/docs/content-configuration#safelisting-classes
|
192
|
+
[safelist example]: https://github.com/abevoelker/simple_form_tailwind_css/issues/4#issuecomment-1232210573
|
193
|
+
|
102
194
|
## Tailwind workarounds
|
103
195
|
|
104
196
|
When using spacing classes such as `space-y-<number>`, Tailwind 2 has [an unfortunate shortcoming](https://github.com/tailwindlabs/tailwindcss/issues/3413) where certain hidden elements disrupt element spacing. Rails's authenticity token unfortunately is one such hidden element that triggers this behavior.
|
@@ -127,4 +219,3 @@ Instead add a wrapper `<div>` around the form elements:
|
|
127
219
|
|
128
220
|
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
129
221
|
|
130
|
-
[Simple Form]: https://github.com/heartcombo/simple_form
|
@@ -2,8 +2,17 @@ module SimpleForm
|
|
2
2
|
module Tailwind
|
3
3
|
class ErrorNotification < SimpleForm::ErrorNotification
|
4
4
|
def render
|
5
|
+
if @options[:color]
|
6
|
+
warn "WARNING SimpleForm::Tailwind's :color error_notification option has " \
|
7
|
+
"been removed due to incompatibility with Tailwind's JIT. " \
|
8
|
+
"Please replace with the new :icon_classes, :message_classes, and " \
|
9
|
+
":border_classes options (see README for an example)."
|
10
|
+
end
|
11
|
+
|
5
12
|
if has_errors?
|
6
|
-
|
13
|
+
icon_classes = @options.fetch(:icon_classes, "h-5 w-5 text-red-400")
|
14
|
+
message_classes = @options.fetch(:message_classes, "text-sm text-red-700")
|
15
|
+
border_classes = @options.fetch(:border_classes, "bg-red-50 border-l-4 border-red-400 p-4")
|
7
16
|
icon = @options.fetch(:icon, "x-circle")
|
8
17
|
|
9
18
|
template.content_tag(
|
@@ -14,14 +23,14 @@ module SimpleForm
|
|
14
23
|
(
|
15
24
|
template.content_tag(
|
16
25
|
:div,
|
17
|
-
template.heroicon(icon, options: { class:
|
26
|
+
template.heroicon(icon, options: { class: icon_classes }),
|
18
27
|
class: "flex-shrink-0"
|
19
28
|
) + template.content_tag(
|
20
29
|
:div,
|
21
30
|
template.content_tag(
|
22
31
|
:p,
|
23
32
|
error_message,
|
24
|
-
class:
|
33
|
+
class: message_classes
|
25
34
|
),
|
26
35
|
class: "ml-3"
|
27
36
|
)
|
@@ -29,7 +38,7 @@ module SimpleForm
|
|
29
38
|
class: "flex"
|
30
39
|
)
|
31
40
|
),
|
32
|
-
class:
|
41
|
+
class: border_classes
|
33
42
|
)
|
34
43
|
end
|
35
44
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: simple_form_tailwind_css
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Abe Voelker
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2022-08-31 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: simple_form
|