FlexContainer.js (3475B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 "use strict"; 6 7 const { 8 createElement, 9 createRef, 10 Fragment, 11 PureComponent, 12 } = require("resource://devtools/client/shared/vendor/react.mjs"); 13 const dom = require("resource://devtools/client/shared/vendor/react-dom-factories.js"); 14 const PropTypes = require("resource://devtools/client/shared/vendor/react-prop-types.mjs"); 15 const { 16 getFormatStr, 17 } = require("resource://devtools/client/inspector/layout/utils/l10n.js"); 18 19 loader.lazyRequireGetter( 20 this, 21 "getNodeRep", 22 "resource://devtools/client/inspector/shared/node-reps.js" 23 ); 24 25 const Types = require("resource://devtools/client/inspector/flexbox/types.js"); 26 27 const { 28 highlightNode, 29 unhighlightNode, 30 } = require("resource://devtools/client/inspector/boxmodel/actions/box-model-highlighter.js"); 31 32 class FlexContainer extends PureComponent { 33 static get propTypes() { 34 return { 35 dispatch: PropTypes.func.isRequired, 36 color: PropTypes.string.isRequired, 37 flexContainer: PropTypes.shape(Types.flexContainer).isRequired, 38 getSwatchColorPickerTooltip: PropTypes.func.isRequired, 39 onSetFlexboxOverlayColor: PropTypes.func.isRequired, 40 }; 41 } 42 43 constructor(props) { 44 super(props); 45 46 this.swatchEl = createRef(); 47 48 this.setFlexboxColor = this.setFlexboxColor.bind(this); 49 } 50 51 componentDidMount() { 52 const tooltip = this.props.getSwatchColorPickerTooltip(); 53 54 let previousColor; 55 tooltip.addSwatch(this.swatchEl.current, { 56 onCommit: this.setFlexboxColor, 57 onPreview: this.setFlexboxColor, 58 onRevert: () => { 59 this.props.onSetFlexboxOverlayColor(previousColor); 60 }, 61 onShow: () => { 62 previousColor = this.props.color; 63 }, 64 }); 65 } 66 67 componentWillUnmount() { 68 const tooltip = this.props.getSwatchColorPickerTooltip(); 69 tooltip.removeSwatch(this.swatchEl.current); 70 } 71 72 setFlexboxColor() { 73 const color = this.swatchEl.current.dataset.color; 74 this.props.onSetFlexboxOverlayColor(color); 75 } 76 77 render() { 78 const { color, flexContainer, dispatch } = this.props; 79 const { nodeFront, properties } = flexContainer; 80 81 return createElement( 82 Fragment, 83 null, 84 dom.div( 85 { 86 className: "flex-header-container-label", 87 }, 88 getNodeRep(nodeFront, { 89 onDOMNodeMouseOut: () => dispatch(unhighlightNode()), 90 onDOMNodeMouseOver: () => dispatch(highlightNode(nodeFront)), 91 }), 92 dom.button({ 93 className: "layout-color-swatch", 94 "data-color": color, 95 ref: this.swatchEl, 96 style: { 97 backgroundColor: color, 98 }, 99 title: getFormatStr("layout.colorSwatch.tooltip", color), 100 }) 101 ), 102 dom.div( 103 { className: "flex-header-container-properties" }, 104 dom.div( 105 { 106 className: "inspector-badge", 107 role: "figure", 108 title: `flex-direction: ${properties["flex-direction"]}`, 109 }, 110 properties["flex-direction"] 111 ), 112 dom.div( 113 { 114 className: "inspector-badge", 115 role: "figure", 116 title: `flex-wrap: ${properties["flex-wrap"]}`, 117 }, 118 properties["flex-wrap"] 119 ) 120 ) 121 ); 122 } 123 } 124 125 module.exports = FlexContainer;