Skip to contentSkip to navigationSkip to topbar
On this page

Create and Style Custom Components


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.


Custom components

custom-components page anchor

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.

1
src
2
├── components
3
└── MyComponent
4
│ └── MyComponent.jsx
5
│ └── MyComponent.Container.js
6
│ └── MyComponent.Styles.js

In this example:

  • MyComponent.jsx will return a React component that could be added by one of Flex's Content.add() APIs
  • MyComponent.Container.js connects the presentational component ( MyComponent.jsx ) to the Redux store
  • MyComponent.Styles.js manages the styles you will apply to your component and its children

Using Twilio Paste

using-twilio-paste page anchor

Flex UI 2.0 leverages Twilio Paste(link takes you to an external page) 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(link takes you to an external page) 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(link takes you to an external page) 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.ts
2
import React from 'react';
3
import { PanelStyles } from './Panel.Styles';
4
5
const Panel = () => {
6
return (
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
};
16
17
export default Panel;
18
19
// Panel.Styles.ts
20
import { styled } from "@twilio/flex-ui";
21
22
export const PanelStyles = styled('div')`
23
text-align: center;
24
background: #D8BFD8;
25
color: #fff;
26
height: 100%;
27
28
ul {
29
Padding-top: 10px;
30
}
31
.first-item {
32
font-size: 30px;
33
}
34
.second-item {
35
font-size: 40px;
36
}
37
.third-item {
38
font-size: 50px;
39
}
40
`;

This approach also introduces useful conventions:

  • Using classnames over individually styled elements favors using HTML elements whose semantics are clearer and more familiar to developers.
  • When an element has a classname, you can infer that it is only styled with CSS and there is no custom functionality.

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.ts
2
import { styled, Theme as FlexTheme } from "@twilio/flex-ui";
3
4
export const SubHeader = styled('div')<{ bgColor: string, theme?: FlexTheme }>`
5
color: ${props => props.theme.tokens.textColors.colorText};
6
background-color: ${props => props.bgColor};
7
font-weight: bold;
8
text-transform: uppercase;
9
`;
10
11
// MyView.tsx
12
render() {
13
return (
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.ts
2
import { injectGlobal } from 'react-emotion';
3
4
injectGlobal`
5
.block {
6
display: block;
7
}
8
.inline-block {
9
display: inline-block;
10
}
11
`;
12
13
// MyPlugin.tsx
14
import '../common/GlobalStyles.ts

Using a CSS file with your plugin

using-a-css-file-with-your-plugin page anchor

You can also declare your styles in a CSS file and import that into a JS file for your global styles.

1
// GlobalStyles.js
2
import { injectGlobal } from 'react-emotion';
3
import global from './global.css';
4
injectGlobal`
5
${global}
6
`;
7
1
/* global.css */
2
.SidePanel-Custom-Container {
3
height: 100%;
4
border: 1px blue;
5
}
6

You can then use displayName to load a stock Flex component (like the SidePanel(link takes you to an external page)) and dynamically set its CSS class name based on the string that you set.

1
<Container>
2
<StyledSidePanel
3
displayName="Custom"
4
themeOverride={theme && theme.OutboundDialerPanel}
5
handleCloseClick={handleClose}
6
title={title}
7
>
8
<ListContainer
9
itemList={itemList}
10
itemType={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.

(warning)

Warning

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.

1
import { FlexPlugin, loadCSS, loadJS } from 'flex-plugin';
2
3
export default class AdminPlugin extends FlexPlugin {
4
constructor() {
5
super('AdminPlugin');
6
}
7
8
public init(flex, manager) {
9
loadCSS('https://dancing-owl-1234.twil.io/assets/test.css');
10
loadJS('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.

1
curl 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
}'
1
public init(flex, manager) {
2
loadCSS(manager.serviceConfiguration.attributes.stylesheet_url);
3
}

Need some help?

Terms of service

Copyright © 2024 Twilio Inc.