UNPKG

react-whitelabel

Version:

A React context and components that make run-time white-labeling of a React app easy.

53 lines (46 loc) 1.6 kB
// Copyright 2020 Thomas Hintz // // This file is part of the react-whitelabel project. // // The react-whitelabel project is free software: you can // redistribute it and/or modify it under the terms of the GNU General // Public License as published by the Free Software Foundation, either // version 3 of the License, or (at your option) any later version OR // under the terms of the MIT license. // // The react-whitelabel project is distributed in the hope that // it will be useful, but WITHOUT ANY WARRANTY; without even the // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR // PURPOSE. // // You should have received a copy of the GNU General Public License // and MIT License along with the react-whitelabel project. If not, // see <https://www.gnu.org/licenses/> and // <https://opensource.org/licenses/MIT>. import React, { Component } from "react"; const WhiteLabelContext = React.createContext(); const Consumer = WhiteLabelContext.Consumer; class WhiteLabelProvider extends Component { render() { return ( <WhiteLabelContext.Provider value={this.props.labels[this.props.selectedLabel]} > {this.props.children} </WhiteLabelContext.Provider> ); } } const withWhiteLabelContext = Component => { return props => { return ( <Consumer> { context => (<Component {...props} label={context} />) } </Consumer> ); }; }; export { WhiteLabelProvider, Consumer as WhiteLabelConsumer, WhiteLabelContext, withWhiteLabelContext };