When you're managing custom components in the Flex UI, you'll likely want to use data from Flex to render your component. For example, you might want your component to display information about the agent's active Task, or your component to inherit color and styling from the Flex Theme configuration instead of setting up unique branding for every component.
The following code sample does not work when mounted to the Supervisor.TaskCanvas
.
Flex includes a withTaskContext()
helper function that adds data about the selected Task to your component. You can read about the properties of the Task object in the autogenerated documentation.
1import React from 'react';2import { withTaskContext } from '@twilio/flex-ui';34// Set text color to white5const TaskSIDText = {6color: "#FFF"7};89class MyComponent extends React.Component {10render() {11// Retrieve Task details12// (`task` will be undefined if there's no task selected in the UI)13const { task } = this.props;14// Render Task SID in component as a test15return <div style={TaskSIDText}>16<p>First, make sure we can access the current Task data.</p>17<p>Task SID: <span style={{ fontWeight: 'bold' }}>{task ? task.taskSid : 'No task selected'}</span></p>18</div>;19}20}2122// The withTaskContext() helper function creates a23// Higher-Order Component that uses the Context API24// to access Task data, then adds the Task data to25// the wrapped component.26export default withTaskContext(MyComponent);
Flex includes a withTheme()
helper function that adds data about the current Theme to your component.
1import React from 'react';2import { withTheme } from '@twilio/flex-ui';3import { styled } from "@twilio/flex-ui";45class MyComponent extends React.Component {6constructor(props) {7super(props);89// Print the current theme object10// You can have a look at at the structure of this object in the console11console.log('Current theme: ', this.props.theme);12}1314render() {15// Return general text in the component16return (17<TaskSID>18<p>Now, we can change the color of the component's background.</p>19</TaskSID>20);21}22}2324// The component that has its background color set25// to the same color as MainHeader background26const TaskSID = styled("div")`27background-color: ${props => props.theme.MainHeader.Container.background};28`;2930// Note the added withTheme() helper function31export default withTheme(MyComponent);
You can use withTheme()
and withTaskContext()
together if you want the Theme data and Task data in your custom component.
1import React from 'react';2import { withTheme, withTaskContext } from '@twilio/flex-ui';3import { styled } from "@twilio/flex-ui";45const TaskSIDText = {6color: "#FFF",7fontWeight: 'bold'8};910class MyComponent extends React.Component {11constructor(props) {12super(props);1314// Print the current theme object15// You can look at the structure of this object in the console16console.log('Current theme: ', this.props.theme);17}18render() {19const { task } = this.props;2021// Print Task SID using styled component `TaskSID` defined below22return (<TaskSID>23<p style={TaskSIDText}>You can access the current Task data AND the current theme. Task SID: {task ? task.taskSid: 'No task selected'}</p>24</TaskSID>);25}26}2728// Styled component that has its text color set to the same color29// as MainHeader background (i.e., red)30const TaskSID = styled("div")`31background-color: ${props => props.theme.MainHeader.Container.background};32`;3334// Note the added withTheme() helper function35export default withTheme(withTaskContext(MyComponent));
For example, if you wanted to display the Task Data on the Flex CRM container, you need to add the following code to your Flex plugin.
1import MyComponent from '<relative_path_to_component_file>';2...3async init(flex, manager) {4flex.CRMContainer.Content.replace(<MyComponent key="mycrm"/>);56}
Refer to Adding Components to Flex UI, Work with Components and Props, and Create and Style Custom Components for more guidance on incorporating the code into your plugin and replacing a Flex component.