Tutorial

06. Writing your own Components

Ok - so you have everything set up and now you want to change how a particular Resource looks.

For instance, you may want to show a google map for location fields, or even present some values as a graph.

You can do this all by writing your custom components.

Example of overriding how dashboard looks:

AdminBroOptions:

const AdminBroOptions = {
  ...
  dashboard: {
    component: AdminBro.bundle('./my-dashboard-component')
  },
  ...
}

Dashboard component: ./my-dashboard-component.jsx

import React from 'react'
import { Box } from 'admin-bro'

const Dashboard = (props) => {
  return (
    <Box>My custom dashboard</Box>
  )
}

export default Dashboard

As you can see AdminBro uses React as a frontend framework. So before you proceed - make sure you know how react works.

Where you can insert your custom components?

Currently, there are 4 places where you can inject components to alter how AdminBro looks:

Requiring component

First of all - you have to require them by using AdminBro.bundle function. What it does - it gives your component an unique ID and sends it to the bundling process.

You can do it like this: { component: AdminBro.bundle('./path-to-your-jsx-or-tsx-file') }

All files required by AdminBro.bundle has to have one default export - the one with your react component.

You can use either .jsx or .tsx extension for your components.

Dependencies

AdminBro bundler gives you the ability to import following dependencies without the need of requiring them in your package.json file:

State management

Routing

Styling

Other

So you can do something like this:

// your-custom-component.jsx
import React from 'react'
import { withRouter } from 'react-router-dom'

const YourComponent (props) => {...}

export default withRouter(YourComponent)

Props passed to components

In your components, you can use props passed by their controlling components.

Currently we have 2 controlling components:

Check out their documentation to see available props

BTW - Dashboard and Pages don't have any controlling component - so no props passed there.

Reusing UI Components of AdminBro

AdminBro gives you the ability to reuse its components. You can do this by simply requiring them:

import { Label } from 'admin-bro'

const YourComponent (props) => {(
  <Label>Some styled text<Label>
)}

Each of the components is described with the playground option, so make sure to check out all the documentation of all the components.

One of the most versatile component is a BasePropertyComponent. It allows you to render any property. Combined with useRecord is a powerful tool for building forms.

Theming

We support Theme compatible with https://system-ui.com/theme standard.

In order to override default colors you an put your values in AdminBroOptions.branding.

Using style props

AdminBro components are supercharged with multiple props which control styling. For instance in order to change color of a Button you can pass backgroundColor (bg) from the Theme like that:

<Button bg="primary60"></Button>

For all possible options visit the Theme description.

Adding custom css to components

If using style props is not enough - you can always pass your custom CSS. So for instance let's assume that you would like to override CSS in a Button component. You can do this like that:

import { Button } from 'admin-bro'

const MyButton = styled(Button)`
  background-color: #ccc;
  color: ${({theme}) => theme.colors.grey100};
  ...
`

We use styled-components under the hood so make sure to check out their docs.

Using other AdminBro frontend classes and objects

AdminBro also exposes following classes:

You can use them like this:

import { ApiClient, ViewHelpers } from 'admin-bro'