Use Twilio Paste with a Flex Plugin
Not every component you build needs to start from scratch. Existing React component libraries can help you use components that have already been built with browser compatibility, responsive screen sizes, and accessibility in mind. Internally, the Flex UI leverages Twilio Paste for many of its components. You can similarly use Paste to create components that start with a similar style to Flex UI's existing layout.
Paste is already a dependency of flex-ui
but if you choose to include it explicitly, ensure the plugin doesn't have multiple versions of Paste in its dependency tree.
Design tokens
Paste design tokens can be referenced as is from within the props of any Paste components.
import { Text } from "@twilio-paste/core/text";
Flex.MainHeader.Content.add(<Text as="p" color="colorTextBrandHighlight" key="some-text">Hello</Text>, {
sortOrder: -1,
align: "end"
});
For more information, please refer to Twilio Paste Tokens.
Creating a custom theme following the official Paste docs may not work. To override default design tokens when using Paste, please see Theming Flex UI - Paste tokens.
Set up your plugin to use Paste
In order to use Twilio Paste inside your plugin, please use Flex.setProviders() as follows which will wrap the Flex UI with the passed Paste theme provider. This will allow you to use Paste elements and design tokens within your plugin.
import { CustomizationProvider } from "@twilio-paste/core/customization";
import { Button as PasteButton } from "@twilio-paste/core/button";
Flex.setProviders({
PasteThemeProvider: CustomizationProvider
});
Flex.AgentDesktopView.Panel1.Content.add(
<div key="A">
<PasteButton key="A" variant="primary">
Paste Button
</PasteButton>
</div>
);
Customize Paste components using elements
To override Paste components styling using Paste's elements
, you can use Flex.setProviders() to wrap a custom Paste theme provider. In this, you can pass it an elements
prop to customize specific components. Refer to Providing Component elements for more details.
You can only replace the elements
property in the following code examples. baseTheme
, theme
, and style
need to be included as provided.
import { CustomizationProvider } from "@twilio-paste/core/customization";
Flex.setProviders({
CustomProvider: (RootComponent) => (props) => {
const pasteProviderProps = {
baseTheme: props.theme?.isLight ? "default" : "dark",
theme: props.theme?.tokens,
style: { minWidth: "100%", height: "100%" },
elements: {
BUTTON: {
backgroundColor: "transparent"
}
}
};
return (
<CustomizationProvider {...pasteProviderProps}>
<RootComponent {...props} />
</CustomizationProvider>
);
}
});
import { CustomizationProvider, CustomizationProviderProps, PasteCustomCSS } from "@twilio-paste/core/customization";
Flex.setProviders({
CustomProvider: (RootComponent) => (props) => {
const pasteProviderProps: CustomizationProviderProps & { style: PasteCustomCSS } = {
baseTheme: props.theme?.isLight ? "default" : "dark",
theme: props.theme?.tokens,
style: { minWidth: "100%", height: "100%" }
elements: {
BUTTON: {
backgroundColor: "transparent"
}
}
};
return (
<CustomizationProvider {...pasteProviderProps}>
<RootComponent {...props} />
</CustomizationProvider>
);
}
});
Need some help?
We all do sometimes; code is hard. Get help now from our support team, or lean on the wisdom of the crowd by visiting Twilio's Stack Overflow Collective or browsing the Twilio tag on Stack Overflow.