|
| 1 | +--- |
| 2 | +title: Customizing CSS |
| 3 | +sort: 1 |
| 4 | +--- |
| 5 | + |
| 6 | +If you want to change the look of the Laravel Form Components to match the style of your own app, you have multiple options. |
| 7 | + |
| 8 | +## Option 1: Use Your Own Tailwind CSS Configuration |
| 9 | +Instead of importing/linking the pre-built `dist/styles.css` from the package, you can import the `src/styles.css` and run every `@apply` rule through your own `tailwind.config.js`. |
| 10 | + |
| 11 | +```css |
| 12 | +/* app.css */ |
| 13 | + |
| 14 | +@tailwind base; |
| 15 | +@tailwind components; |
| 16 | +@tailwind utilities; |
| 17 | + |
| 18 | +@import '../../vendor/rawilk/laravel-form-components/resources/js/laravel-form-components-styles/src/styles.css'; |
| 19 | + |
| 20 | +/* override our styles here */ |
| 21 | +``` |
| 22 | + |
| 23 | +> {note} If you choose this option, make sure you have the [required variants](#required-variants) included in your `tailwind.config.js` configuration. |
| 24 | +
|
| 25 | +## Option 2: Override Only Portions In Your CSS |
| 26 | +If you only want to tinker with certain apsects of the components but like to keep the CSS in sync with future package updates, nothing stops you from overriding only certain CSS rules with your own tweaks. Most DOM elements have their own custom class names. |
| 27 | + |
| 28 | +Let's say your inputs aren't rounded, and you want to remove the border radius from them. To do that, you can write your own CSS for this class: |
| 29 | + |
| 30 | +```css |
| 31 | +/* app.css */ |
| 32 | +... |
| 33 | + |
| 34 | +@import '../../vendor/rawilk/laravel-form-components/resources/js/laravel-form-components-styles/dist/styles.css'; |
| 35 | + |
| 36 | +.form-text { |
| 37 | + @apply rounded-none; |
| 38 | +} |
| 39 | +``` |
| 40 | + |
| 41 | +## Option 3: Copy the CSS To Your Own Project |
| 42 | +If you want full-control, you can always copy the `src/styles.css` to your own project and go wild. In this example, we renamed the file to `custom/laravel-form-components.css`. |
| 43 | +Beware: you will have to manually keep this CSS in sync with changes in future package updates: |
| 44 | + |
| 45 | +```css |
| 46 | +/* app.css */ |
| 47 | + |
| 48 | +... |
| 49 | + |
| 50 | +@import 'custom/laravel-form-components.css'; |
| 51 | +``` |
| 52 | + |
| 53 | +Let's say you wanted to change the background color of disabled inputs. You could do so like this in the file you just created with the pasted in styles from the package: |
| 54 | + |
| 55 | +```css |
| 56 | +/* custom/laravel-form-components.css */ |
| 57 | + |
| 58 | +input[disabled], |
| 59 | +textarea[disabled], |
| 60 | +select[disabled] { |
| 61 | + @apply bg-gray-100; |
| 62 | + |
| 63 | + /* default styles from the package */ |
| 64 | + /*@apply bg-blue-gray-50 cursor-not-allowed;*/ |
| 65 | +} |
| 66 | + |
| 67 | +... |
| 68 | +``` |
| 69 | + |
| 70 | +## Required Variants |
| 71 | +If you choose [Option 1](#option-1-use-your-own-tailwind-css-configuration), you will need the following color variants added inside your `tailwind.config.js` file: |
| 72 | + |
| 73 | +```js |
| 74 | +// tailwind.config.js |
| 75 | + |
| 76 | +const colors = require('tailwindcss/colors'); |
| 77 | + |
| 78 | +module.exports = { |
| 79 | + // ... |
| 80 | + theme: { |
| 81 | + extend: { |
| 82 | + colors: { |
| 83 | + 'blue-gray': colors.blueGray, |
| 84 | + 'cool-gray': colors.coolGray, |
| 85 | + }, |
| 86 | + }, |
| 87 | + }, |
| 88 | +}; |
| 89 | +``` |
| 90 | + |
| 91 | +This will extend the default tailwind color palette to include the `blue-gray` and `cool-gray` color variants. |
| 92 | + |
| 93 | +> {note} If you have a custom color palette configured, you will need to make sure you have the `blue` and `red` colors configured as well, with all |
| 94 | +> levels available (`50` through `900`). |
| 95 | +
|
| 96 | +Certain components make use of a custom utility class called `outline-blue-gray`, which adds a 2px dotted outline around the element when focused. If you opt to not add this outline variant to your configuration, it will not affect building the CSS since the package's stylesheet does not reference it; the outline variant is only rendered directly onto the elements it's used for. If you want the outline to show up, you can add the following to your `tailwind.config.js` file: |
| 97 | + |
| 98 | +```js |
| 99 | +// tailwind.config.js |
| 100 | + |
| 101 | +const colors = require('tailwindcss/colors'); |
| 102 | + |
| 103 | +module.exports = { |
| 104 | + // ... |
| 105 | + |
| 106 | + theme: { |
| 107 | + extend: { |
| 108 | + colors: { |
| 109 | + // see above snippet for colors |
| 110 | + // ... |
| 111 | + }, |
| 112 | + |
| 113 | + outline: { |
| 114 | + 'blue-gray': [`2px dotted ${colors.blueGray['500']}`, '2px'], |
| 115 | + }, |
| 116 | + }, |
| 117 | + }, |
| 118 | +}; |
| 119 | +``` |
| 120 | + |
| 121 | +## Purge CSS/Tailwind JIT |
| 122 | + |
| 123 | +Purge CSS is useful for trimming out unused styles from your stylesheets to reduce your overall build size. To ensure |
| 124 | +the class styles from this package don't get purged from your production build, you should add the following to your |
| 125 | +purge css content configuration: |
| 126 | + |
| 127 | +> {note} The following code snippet is for a TailwindCSS build configuration using a `tailwind.config.js` file in the build. |
| 128 | +
|
| 129 | +```js |
| 130 | +module.exports = { |
| 131 | + // ... |
| 132 | + purge: { |
| 133 | + content: [ |
| 134 | + // Typical laravel app purge css content |
| 135 | + './app/**/*.php', |
| 136 | + './resources/**/*.php', |
| 137 | + './resources/**/*.js', |
| 138 | + |
| 139 | + // Make sure you add these lines |
| 140 | + './vendor/rawilk/laravel-form-components/src/**/*.php', |
| 141 | + './vendor/rawilk/laravel-form-components/resources/**/*.php', |
| 142 | + './vendor/rawilk/laravel-form-components/resources/js/*.js', |
| 143 | + ], |
| 144 | + }, |
| 145 | +}; |
| 146 | +``` |
| 147 | + |
| 148 | +This configuration should also work when using the JIT compiler from Tailwind. |
| 149 | + |
| 150 | +If some styles are still being purged, it may be useful to wrap the import statement of the package's stylesheet |
| 151 | +in a `/* purgecss start ignore */`: |
| 152 | + |
| 153 | +```css |
| 154 | +/* purgecss start ignore */ |
| 155 | +@import '../../vendor/rawilk/laravel-form-components/resources/js/laravel-form-components-styles/dist/styles.css'; |
| 156 | +/* purgecss end ignore */ |
| 157 | +``` |
0 commit comments