LearnMoreLink.js (2022B)
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 Component, 9 } = require("resource://devtools/client/shared/vendor/react.mjs"); 10 const PropTypes = require("resource://devtools/client/shared/vendor/react-prop-types.mjs"); 11 const { 12 p, 13 a, 14 } = require("resource://devtools/client/shared/vendor/react-dom-factories.js"); 15 16 const { openDocLink } = require("resource://devtools/client/shared/link.js"); 17 18 /** 19 * Localization friendly component for rendering a block of text with a "Learn more" link 20 * as a part of it. 21 */ 22 class LearnMoreLink extends Component { 23 static get propTypes() { 24 return { 25 className: PropTypes.string, 26 href: PropTypes.string, 27 learnMoreStringKey: PropTypes.string.isRequired, 28 l10n: PropTypes.object.isRequired, 29 messageStringKey: PropTypes.string.isRequired, 30 onClick: PropTypes.func, 31 }; 32 } 33 34 static get defaultProps() { 35 return { 36 href: "#", 37 learnMoreStringKey: null, 38 l10n: null, 39 messageStringKey: null, 40 onClick: LearnMoreLink.openDocOnClick, 41 }; 42 } 43 44 static openDocOnClick(event) { 45 event.preventDefault(); 46 openDocLink(event.target.href); 47 } 48 49 render() { 50 const { 51 className, 52 href, 53 learnMoreStringKey, 54 l10n, 55 messageStringKey, 56 onClick, 57 } = this.props; 58 const learnMoreString = l10n.getStr(learnMoreStringKey); 59 const messageString = l10n.getFormatStr(messageStringKey, learnMoreString); 60 61 // Split the paragraph string with the link as a separator, and include the link into 62 // results. 63 const re = new RegExp(`(\\b${learnMoreString}\\b)`); 64 const contents = messageString.split(re); 65 contents[1] = a({ className: "link", href, onClick }, contents[1]); 66 67 return p( 68 { 69 className, 70 }, 71 ...contents 72 ); 73 } 74 } 75 76 module.exports = LearnMoreLink;