UNPKG

ns-fivem-react-boilerplate

Version:
180 lines (128 loc) • 4.25 kB
# NS FiveM React Boilerplate A modern template for developing FiveM NUI interfaces using React, TypeScript, Vite, and TailwindCSS. ## ✨ Features - **React 19** with TypeScript - **Vite** for fast builds and hot reload - **TailwindCSS 4.0** for styling - **Custom hooks** for FiveM NUI integration - **ESLint** configured for React - **Project structure** optimized for FiveM - **Visibility provider** for interface management ## šŸš€ Quick Start You can create a new project using this template in two ways: ### Method 1: Using npx (recommended) ```bash npx ns-fivem-react-boilerplate my-fivem-resource cd my-fivem-resource npm install ``` ### Method 2: Manual clone ```bash git clone https://github.com/Nebula-Studios/ns-fivem-react-boilerplate.git my-fivem-resource cd my-fivem-resource rm -rf .git npm install ``` ## šŸ“‹ Project Structure ``` my-fivem-resource/ ā”œā”€ā”€ src/ │ ā”œā”€ā”€ components/ # React components │ │ └── App.tsx │ ā”œā”€ā”€ hooks/ # Custom hooks │ │ └── useNuiEvent.ts │ ā”œā”€ā”€ providers/ # Context providers │ │ └── VisibilityProvider.tsx │ ā”œā”€ā”€ utils/ # Utilities │ │ ā”œā”€ā”€ debugData.ts │ │ ā”œā”€ā”€ fetchNui.ts │ │ └── misc.ts │ ā”œā”€ā”€ index.css # Global styles │ ā”œā”€ā”€ main.tsx # Entry point │ └── vite-env.d.ts # TypeScript definitions ā”œā”€ā”€ index.html ā”œā”€ā”€ package.json ā”œā”€ā”€ vite.config.ts ā”œā”€ā”€ tsconfig.json └── postcss.config.mjs ``` ## šŸ› ļø Available Scripts ```bash # Start development server npm start # Build for development with watch mode (for FiveM) npm run start:game # Production build npm run build # Preview build npm run preview ``` ## šŸŽÆ FiveM Usage ### 1. Development During development, use: ```bash npm run start:game ``` This command will build the project in watch mode, automatically updating files when you make changes. ### 2. NUI Communication The template includes hooks and utilities for communicating with the FiveM client: #### useNuiEvent Hook ```tsx import { useNuiEvent } from "./hooks/useNuiEvent"; function MyComponent() { useNuiEvent<{ message: string }>("showNotification", (data) => { console.log(data.message); }); return <div>My Component</div>; } ``` #### fetchNui Utility ```tsx import { fetchNui } from "./utils/fetchNui"; const handleClick = async () => { try { const response = await fetchNui<{ success: boolean }>("myCallback", { data: "some data", }); console.log(response.success); } catch (error) { console.error("Error:", error); } }; ``` ### 3. Visibility Management The `VisibilityProvider` automatically manages interface visibility: ```tsx // The interface will automatically hide when ESC is pressed // or when it receives the 'setVisible' event with false ``` ## šŸŽØ Customization ### TailwindCSS The project uses TailwindCSS 4.0. You can customize colors and themes by modifying the CSS configuration file in `src/index.css`. ### TypeScript TypeScript configurations are optimized for React and Vite. You can modify `tsconfig.json` for your specific needs. ## šŸ“¦ Build and Deployment 1. **For FiveM development:** ```bash npm run start:game ``` 2. **For production:** ```bash npm run build ``` Built files will be in the `dist/` folder and can be copied to your FiveM resource. ## šŸ¤ Contributing 1. Fork the project 2. Create a feature branch (`git checkout -b feature/AmazingFeature`) 3. Commit your changes (`git commit -m 'Add some AmazingFeature'`) 4. Push to the branch (`git push origin feature/AmazingFeature`) 5. Open a Pull Request ## šŸ“„ License This project is released under the MIT License. See the `LICENSE` file for more details. ## šŸ™ Acknowledgments - [React](https://reactjs.org/) - [Vite](https://vitejs.dev/) - [TailwindCSS](https://tailwindcss.com/) - [FiveM](https://fivem.net/) --- Made with ā¤ļø for the FiveM community