As with most development, there are many ways for you to approach your customizations on top of Flex. These guidelines are based on our experience building Flex Plugins.
When building a new component, we recommend following the conventions demoed by the Plugin Builder. Create a directory within /src/components
, and then a trio of files to represent the content and styles of your component.
1src2├── components3│ └── MyComponent4│ │ └── MyComponent.jsx5│ │ └── MyComponent.Container.js6│ │ └── MyComponent.Styles.js
In this example:
MyComponent.jsx
will return a React component that could be added by one of Flex's Content.add()
APIsMyComponent.Container.js
connects the presentational component (MyComponent.jsx
) to the Redux storeMyComponent.Styles.js
manages the styles you will apply to your component and its childrenFlex UI 2.0 leverages Twilio Paste for many of its components. To learn more, refer to Use Twilio Paste with a Flex Plugin.
We've found it easier to manage plugin development when your styles and your code are bundled together as part of your plugin. We recommend using Emotion for managing the styles of your custom components. If you choose to use Emotion, make sure to include it in your package.json
dependencies.
We suggest defining a component-level style wrapper for each of your components. However, if the same styles are applied on the same type of element or you want to do dynamic styling, create separate styled components for better reusability.
Following the file structure above, we recommend keeping your styles alongside your components in files such as MyComponent.Styles.js
.
There are many ways you can use Emotion to style your components. We suggest using styled
to define a component-level style wrapper. This styled
component will include all of the styles for your main component and its children.
1// Panel.ts2import React from 'react';3import { PanelStyles } from './Panel.Styles';45const Panel = () => {6return (7<PanelStyles>8<ul>9<li className="first-item">A</li>10<li className="second-item">B</li>11<li className="third-item">C</li>12</ul>13</PanelStyles>14);15};1617export default Panel;1819// Panel.Styles.ts20import { styled } from "@twilio/flex-ui";2122export const PanelStyles = styled('div')`23text-align: center;24background: #D8BFD8;25color: #fff;26height: 100%;2728ul {29Padding-top: 10px;30}31.first-item {32font-size: 30px;33}34.second-item {35font-size: 40px;36}37.third-item {38font-size: 50px;39}40`;
This approach also introduces useful conventions:
styled
can also be used to implement dynamic styles based on props. The Flex theme is automatically accessible within styled
components via props.theme
because Flex UI wraps all of its components in a ThemeProvider
. You can also use this approach to pass in custom props, like bgColor
in the example below.
1// MyView.Styles.ts2import { styled, Theme as FlexTheme } from "@twilio/flex-ui";34export const SubHeader = styled('div')<{ bgColor: string, theme?: FlexTheme }>`5color: ${props => props.theme.tokens.textColors.colorText};6background-color: ${props => props.bgColor};7font-weight: bold;8text-transform: uppercase;9`;1011// MyView.tsx12render() {13return (14<div>15<SubHeader bgColor="red">This font color should be red.</SubHeader>16<SubHeader bgColor="blue">This font color should be blue.</SubHeader>17</div>18);19}
To add global styles to your plugin, use injectGlobal
from Emotion. We suggest keeping a separate file for your global styles and importing it in your top-level plugin.
1// GlobalStyles.ts2import { injectGlobal } from 'react-emotion';34injectGlobal`5.block {6display: block;7}8.inline-block {9display: inline-block;10}11`;1213// MyPlugin.tsx14import '../common/GlobalStyles.ts
You can also declare your styles in a CSS file and import that into a JS file for your global styles.
1// GlobalStyles.js2import { injectGlobal } from 'react-emotion';3import global from './global.css';4injectGlobal`5${global}6`;7
1/* global.css */2.SidePanel-Custom-Container {3height: 100%;4border: 1px blue;5}6
You can then use displayName
to load a stock Flex component (like the SidePanel) and dynamically set its CSS class name based on the string that you set.
1<Container>2<StyledSidePanel3displayName="Custom"4themeOverride={theme && theme.OutboundDialerPanel}5handleCloseClick={handleClose}6title={title}7>8<ListContainer9itemList={itemList}10itemType={itemType}11/>12</StyledSidePanel>13</Container>14
In this example, the styles you've declared within .SidePanel-Custom-Container
in your CSS file will be applied.
When you use CSS in Flex, do not use Twilio-
in any of your class names.
It may not always be practical to define your styles alongside each component. Maybe you are using shared stylesheets across a suite of applications. Or maybe you're building multiple plugins that should share a central CSS asset.
The loadCSS
and loadJS
methods from flex-plugin
can be used in these situations to load external resources when initializing your plugin.
1import { FlexPlugin, loadCSS, loadJS } from 'flex-plugin';23export default class AdminPlugin extends FlexPlugin {4constructor() {5super('AdminPlugin');6}78public init(flex, manager) {9loadCSS('https://dancing-owl-1234.twil.io/assets/test.css');10loadJS('https://dancing-owl-1234.twil.io/assets/test.js');11}12}
One difficulty with this approach is ensuring that your external URLs can be used in whichever environment you're deploying your plugin. For example, you wouldn't want to re-build your plugin if the styles depend on versioned URLs or if the assets are different in your development vs. production environment.
One approach is to use the Flex Configuration API to store the URLs as attributes, and then to reference these attributes from within your plugin.
1curl https://flex-api.twilio.com/v1/Configuration -X POST -u ACxxx:auth_token \2-H 'Content-Type: application/json' \3-d '{4"account_sid": "ACxxx",5"attributes": {6"stylesheet_url": "https://my-external-site.com/styles.css"7}8}'
1public init(flex, manager) {2loadCSS(manager.serviceConfiguration.attributes.stylesheet_url);3}