diff --git a/src/components/SdkModal.tsx b/src/components/SdkModal.tsx
index ce16d96..b8f4145 100644
--- a/src/components/SdkModal.tsx
+++ b/src/components/SdkModal.tsx
@@ -4,6 +4,7 @@ import { useMemo, useState, useEffect } from '@wordpress/element';
import { useCopyToClipboard } from '@wordpress/compose';
import { Flag } from '../../types';
import { __ } from '@wordpress/i18n';
+import TsSupport from './TsSupport';
interface SdkModalProps {
item: Flag;
@@ -42,14 +43,17 @@ const SdkModal = ({ item, closeSdkModal }: SdkModalProps): JSX.Element => {
const jsSnippet = useMemo(() => {
return `import domReady from '@wordpress/dom-ready';
domReady(function () {
- if (window.mrFeatureFlags.isEnabled('${item.name}')) {
+ if (
+ typeof window?.mrFeatureFlags !== 'undefined' &&
+ window.mrFeatureFlags.isEnabled('Menus')
+ ) {
// js code goes here...
}
- });`;
+});`;
}, [item.name]);
const phpSnippet = useMemo(() => {
- return `if ( MR\\FeatureFlags\\Utils::is_enabled( '${item.name}' ) ) {
+ return `if ( class_exists( 'MR\\FeatureFlags\\Utils' ) && MR\\FeatureFlags\\Utils::is_enabled( '${item.name}' ) ) {
// php code goes here...
}`;
}, [item.name]);
@@ -73,35 +77,35 @@ domReady(function () {
-
+
{__('JavaScript Snippet', 'mr-feature-flags')}
-
+
+
+
+
);
diff --git a/src/components/Snippet.tsx b/src/components/Snippet.tsx
index ccb8dbe..25bb9a9 100644
--- a/src/components/Snippet.tsx
+++ b/src/components/Snippet.tsx
@@ -1,9 +1,15 @@
import SyntaxHighlighter from 'react-syntax-highlighter';
import { a11yDark } from 'react-syntax-highlighter/dist/esm/styles/hljs';
-const Snippet = ({ data }: { data: string }): JSX.Element => {
+const Snippet = ({
+ data,
+ language,
+}: {
+ data: string;
+ language: string;
+}): JSX.Element => {
return (
-
+
{data}
);
diff --git a/src/components/TsSupport.tsx b/src/components/TsSupport.tsx
new file mode 100644
index 0000000..cd412be
--- /dev/null
+++ b/src/components/TsSupport.tsx
@@ -0,0 +1,34 @@
+import { useMemo } from '@wordpress/element';
+import { __ } from '@wordpress/i18n';
+
+import Snippet from './Snippet';
+
+const TsSupport = (): JSX.Element => {
+ const tsSnippet = useMemo(() => {
+ return `declare namespace mrFeatureFlags {
+ export interface FeatureFlagProps {
+ isEnabled: (flag: string) => boolean;
+ }
+}
+declare global {
+ interface Window {
+ mrFeatureFlags: mrFeatureFlags.FeatureFlagProps;
+ }
+}
+export {};`;
+ }, []);
+
+ return (
+
+
{__('TypeScript support', 'mr-feature-flags')}
+
+ Create a file named{' '}
+ flags.d.ts at the
+ root of the plugin / theme and add below declaration.
+
+
+
+ );
+};
+
+export default TsSupport;
diff --git a/src/styles/settings.scss b/src/styles/settings.scss
index 3b90bab..450f83c 100644
--- a/src/styles/settings.scss
+++ b/src/styles/settings.scss
@@ -21,3 +21,10 @@
#mr-feature-flag-submit-controls {
margin-top: 20px;
}
+
+.mr-feature-flags-slug {
+ background-color: #007cba;
+ border-radius: 6px;
+ padding: 3px;
+ color: #fff;
+}