Creating components
When building your UI with GamefaceUI, you may want to split your code into reusable custom components. To facilitate this, the GamefaceUI boilerplate includes a dedicated folder for custom components. It is recommended to place your custom components in this folder.
Guidelines for creating custom components
The steps outlined below are not mandatory for creating custom components but are recommended as they follow a structured approach and enable features like alias imports.
To create a new component, use the /src/custom-components
folder.
Example
Let’s create a TopBar
component for the Hud
view.
-
Create a folder for the view where the component will be used. For this example, create
/src/custom-components/Hud
. -
Inside this folder, create another folder for the component:
/src/custom-components/Hud/TopBar
. -
Set up the component’s source files in this folder:
-
Create a
TopBar.tsx
file for the component’s logic and functionality. -
Create a
TopBar.module.css
file for the component’s styles.
-
-
Add styles to the
TopBar.module.css
file:.testClass {color: red;}At this point, your file structure should look like this:
Directorysrc
Directorycustom-components
DirectoryHud
DirectoryTopBar
- TopBar.tsx
- TopBar.module.css
Directoryviews
Directoryhud
- Hud.tsx
- …
- …
-
Import the styles as a module in the
TopBar.tsx
file and apply them using the CSS module syntax:class={styles.testClass}
./src/custom-components/Hud/TopBar/TopBar.tsx import styles from './TopBar.module.css'const TopBar = () => {return <div class={styles.testClass}></div>} -
Once your component is ready, you can use it in your view. For example, to use
TopBar
in theHud
view, import it as shown below:/src/views/hud/Hud.tsx import TopBar from '@custom-components/TopBar/TopBar';const Hud = () => {return <TopBar />;}