|
| 1 | +# Tailwind CSS |
| 2 | + |
| 3 | +## Install |
| 4 | + |
| 5 | +In your project root directory, run the following command to add Tailwind CSS to your project: |
| 6 | +```bash |
| 7 | +npx add-dependencies tailwindcss postcss dotenv |
| 8 | +``` |
| 9 | + |
| 10 | +## Configure |
| 11 | + |
| 12 | +Create a `tailwind.config.js` file in your project root directory with the following content: |
| 13 | +```javascript |
| 14 | +require('dotenv').config() |
| 15 | + |
| 16 | +module.exports = { |
| 17 | + content: { |
| 18 | + files: [ |
| 19 | + // Adapt this to your needs, this works with a project created from the NUI template. |
| 20 | + `${process.env.NUI_PROJECT_ROOT}/frontend/**/*.{cpp,hpp}`, |
| 21 | + `${process.env.NUI_PROJECT_ROOT}/static/**/*.{html,js}` |
| 22 | + ] |
| 23 | + }, |
| 24 | + theme: { |
| 25 | + extend: {}, |
| 26 | + }, |
| 27 | + plugins: [], |
| 28 | +} |
| 29 | +``` |
| 30 | + |
| 31 | +Create a `.postcssrc` file in your project root directory with the following content: |
| 32 | +```json |
| 33 | +{ |
| 34 | + "plugins": { |
| 35 | + "tailwindcss": {} |
| 36 | + } |
| 37 | +} |
| 38 | +``` |
| 39 | + |
| 40 | +Add the Tailwind directives to your CSS file: |
| 41 | +nui-template/static/styles/main.css: |
| 42 | +```css |
| 43 | +@tailwind base; |
| 44 | +@tailwind components; |
| 45 | +@tailwind utilities; |
| 46 | +``` |
| 47 | + |
| 48 | +## Enable in CMake |
| 49 | + |
| 50 | +In your backend CMakeLists.txt in your `nui_add_emscripten_target` call, add the ENABLE_TAILWIND option: |
| 51 | +```cmake |
| 52 | +nui_add_emscripten_target( |
| 53 | + ... |
| 54 | + ENABLE_TAILWIND |
| 55 | +) |
| 56 | +``` |
| 57 | + |
| 58 | +## Use in Your Application |
| 59 | + |
| 60 | +```cpp |
| 61 | +#include <frontend/main_page.hpp> |
| 62 | + |
| 63 | +#include <nui/frontend/elements.hpp> |
| 64 | +#include <nui/frontend/attributes.hpp> |
| 65 | + |
| 66 | +Nui::ElementRenderer MainPage::render() |
| 67 | +{ |
| 68 | + using namespace Nui; |
| 69 | + using namespace Nui::Elements; |
| 70 | + using namespace Nui::Attributes; |
| 71 | + using Nui::Elements::div; // because of the global div. |
| 72 | + |
| 73 | + // use of the tailwind classes: |
| 74 | + return body{}(h1{class_ = "text-3xl font-bold underline"}("Hello, Tailwind!")); |
| 75 | +} |
| 76 | +``` |
0 commit comments