Use a custom theme
Ory Elements allows you to customize the look and feel of the default UI components by using a custom theme. This guide will show you how to create and apply a custom theme to your Ory Elements components.
Understanding CSS theming in Ory Elements
Ory Elements uses CSS variables to define the styles of its components. These variables are defined in a hierarchical manner, allowing you to override base variables that are used in many components at once or variables that are only used in specifc components.
To understand this hierarchical structure, consider the following diagram:
This diagram shows how the ui-50 variable is used as a base for other variables like
interface-background-default-primary-hover and interface-background-default-secondary. These variables are then used in
specific components like buttons, checkboxes, and toggles. By overriding the ui-50 variable, you can change the look and feel of
all components that use it.
Creating a custom theme
Now, if you want to change the color in all of these components, you can simply override the ui-50 variable in your CSS files.
This will affect all components that use this variable, allowing you to create a consistent look and feel across your application.
A mix is also possible, where you can override the core variables, and then target specific components with additional styles. For
example, you can override the ui-50 variable and then add specific styles for buttons. The default theme declares its variables
on the .ory-elements class, so override them on the same selector:
.ory-elements {
--ui-50: #f0f4f8; /* Change the base color */
--button-secondary-background-hover: #e0e4e8; /* Change the button hover color */
--radius-buttons: 0.5rem; /* Round the button corners */
--radius-cards: 1rem; /* Round the card corners */
}
The corner radius variables are --radius-buttons, --radius-forms, --radius-cards, --radius-general, --radius-branding,
and --radius-identifier.
For a full list of CSS variables used in Ory Elements, you can refer to the CSS Reference document.
Applying the custom theme
To apply the custom theme to your Ory Elements components, you need to import the CSS file in your application. You can do this in your main application file or in a specific component where you want to use the custom theme. Here is an example of how to import the CSS file in a Next.js application:
import "../styles/theme.css" // Import the custom theme CSS file
function MyApp({ Component, pageProps }) {
return <Component {...pageProps} />
}
export default MyApp
Using the default theme with custom components
When building custom components, you can still leverage the default theme by using the CSS variables defined in Ory Elements. The
default theme's styles are scoped to the .ory-elements CSS class. This means that if you wrap your custom components in a
container with this class, they will inherit the default theme styles.
Replacing components
To change the markup of a part of the UI, pass your own component for its slot in the components prop of a flow component. The
slots are the properties of the OryFlowComponents type,
grouped as Card, Node, Form, Message, and Page.
<Login flow={flow} config={config} components={{ Card: { Header: MyCardHeader }, Node: { Button: MyButton } }} />
Slot names don't repeat the name of their group. The header of the card is Card.Header, not Card.CardHeader.
The @ory/elements-react package contains a list of every slot with the props it receives and its default component. The list is
generated from the types, so it matches the version you have installed:
node_modules/@ory/elements-react/agents/slots.mdnode_modules/@ory/elements-react/agents/slots.json, the same data for tools
Versions that predate these files don't have the agents directory. Use the
OryFlowComponents type reference instead.
Customize consent resources
The Ory Elements Consent component displays the full URI of each requested OAuth 2.0 resource below the scopes. Resources are
informational: they do not add checkboxes or form values. Your consent handler decides which resources to grant, as described in
the resource indicator guide.
Override Node.ConsentResource in the components passed to OryProvider to customize each resource's appearance. The component
receives resource, the full URI, and node, the corresponding text node. For example:
import type { OryNodeConsentResourceProps } from "@ory/elements-react"
function ConsentResource({ resource }: OryNodeConsentResourceProps) {
return <p className="consent-resource">{resource}</p>
}
const components = {
Node: { ConsentResource },
}
Pass components to your existing OryProvider. This changes how resources are displayed; the consent handler still determines
which audiences are granted.