Skip to content

Commit

Permalink
feat(feature-flags): implement feature flags for cwc (#11081)
Browse files Browse the repository at this point in the history
  • Loading branch information
annawen1 committed Nov 1, 2023
1 parent 8173018 commit 9b0c96d
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
33 changes: 33 additions & 0 deletions packages/carbon-web-components/.storybook/manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,36 @@ import yourTheme from './theme';
addons.setConfig({
theme: yourTheme,
});

/**
* Conditionally generate CSS to hide a component based on its corresponding
* feature flag environment variable.
*
* @param {*} envVar
* Environment variable to check.
* @param {*} cssId
* CSS ID for selector.
* @returns
*/
const getCss = (envVar, cssId) => {
return envVar !== 'true'
? `button[id^="${cssId}"] { display: none !important; }\n`
: '';
};

// Build string of CSS rules.
let css = '';
if (!process.env.CDS_FLAGS_ALL) {
css += getCss(
process.env.CDS_EXPERIEMENTAL_COMPONENT_NAME,
'components-experimental-component-name'
);
}

// Inject any CSS rules into the page.
if (css.length) {
const head = document.head || document.getElementsByTagName('head')[0];
const style = document.createElement('style');
head.appendChild(style);
style.appendChild(document.createTextNode(css));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* @license
*
* Copyright IBM Corp. 2023
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/

/**
* This file contains the list of the default values of compile-time feature flags.
*/

/**
* This flag will determine if all feature flags should be enabled
*
* @type {boolean}
*/
export const CDS_FLAGS_ALL: boolean =
process!.env.CDS_FLAGS_ALL === 'true' || false;

/**
* Enables experimental component
*
* @type {boolean}
*/
export const CDS_EXPERIEMENTAL_COMPONENT_NAME: boolean =
process!.env.CDS_EXPERIEMENTAL_COMPONENT_NAME === 'true' ||
CDS_FLAGS_ALL ||
false;

0 comments on commit 9b0c96d

Please sign in to comment.