- {!isRoot && showKey && (
+ {(!isRoot || name) && showKey && (
<>
{computedKey}
:
@@ -45,6 +49,8 @@ const buildValue = (
/** Display a leaf key-value pair with appropriate styles. */
export const ObjectValue = (props: ObjectValueProps) => {
+ const context = React.useContext(ObjectInspectorContext);
+ const name = context?.name || "";
const { ast, theme, showKey, colorScheme, className, ...html } = props;
const { themeClass } = useTheme({ theme, colorScheme }, styles);
const [asyncValue, setAsyncValue] = useState();
@@ -61,6 +67,7 @@ export const ObjectValue = (props: ObjectValueProps) => {
`Promise { "${await getPromiseState(promise)}" }`,
styles.key,
showKey,
+ name,
ast.depth
)
);
@@ -77,6 +84,7 @@ export const ObjectValue = (props: ObjectValueProps) => {
String(ast.value),
styles.number,
showKey,
+ name,
ast.depth
);
} else if (typeof ast.value === "boolean") {
@@ -86,6 +94,7 @@ export const ObjectValue = (props: ObjectValueProps) => {
String(ast.value),
styles.boolean,
showKey,
+ name,
ast.depth
);
} else if (typeof ast.value === "string") {
@@ -95,6 +104,7 @@ export const ObjectValue = (props: ObjectValueProps) => {
`"${ast.value}"`,
styles.string,
showKey,
+ name,
ast.depth
);
} else if (typeof ast.value === "undefined") {
@@ -104,6 +114,7 @@ export const ObjectValue = (props: ObjectValueProps) => {
"undefined",
styles.undefined,
showKey,
+ name,
ast.depth
);
} else if (typeof ast.value === "symbol") {
@@ -113,6 +124,7 @@ export const ObjectValue = (props: ObjectValueProps) => {
ast.value.toString(),
styles.string,
showKey,
+ name,
ast.depth
);
} else if (typeof ast.value === "function") {
@@ -122,12 +134,20 @@ export const ObjectValue = (props: ObjectValueProps) => {
`${ast.value.name}()`,
styles.key,
showKey,
+ name,
ast.depth
);
} else if (typeof ast.value === "object") {
if (ast.value === null) {
// Null
- value = buildValue(ast.key, "null", styles.null, showKey, ast.depth);
+ value = buildValue(
+ ast.key,
+ "null",
+ styles.null,
+ showKey,
+ name,
+ ast.depth
+ );
} else if (Array.isArray(ast.value)) {
// Array
value = buildValue(
@@ -135,6 +155,7 @@ export const ObjectValue = (props: ObjectValueProps) => {
`Array(${ast.value.length})`,
styles.key,
showKey,
+ name,
ast.depth
);
} else if (ast.value instanceof Date) {
@@ -144,6 +165,7 @@ export const ObjectValue = (props: ObjectValueProps) => {
`Date ${ast.value.toString()}`,
styles.value,
showKey,
+ name,
ast.depth
);
} else if (ast.value instanceof RegExp) {
@@ -153,6 +175,7 @@ export const ObjectValue = (props: ObjectValueProps) => {
ast.value.toString(),
styles.regex,
showKey,
+ name,
ast.depth
);
} else if (ast.value instanceof Error) {
@@ -162,11 +185,12 @@ export const ObjectValue = (props: ObjectValueProps) => {
ast.value.toString(),
styles.error,
showKey,
+ name,
ast.depth
);
} else if (isObject(ast.value)) {
// Object
- value = buildValue(ast.key, "{…}", styles.key, showKey, ast.depth);
+ value = buildValue(ast.key, "{…}", styles.key, showKey, name, ast.depth);
} else {
// WeakMap, WeakSet, Custom Classes, etc
value = buildValue(
@@ -174,6 +198,7 @@ export const ObjectValue = (props: ObjectValueProps) => {
ast.value.constructor.name,
styles.key,
showKey,
+ name,
ast.depth
);
}
diff --git a/components/ObjectInspector/src/stories/Features.stories.tsx b/components/ObjectInspector/src/stories/Features.stories.tsx
index 55a2a0f..20c1502 100644
--- a/components/ObjectInspector/src/stories/Features.stories.tsx
+++ b/components/ObjectInspector/src/stories/Features.stories.tsx
@@ -72,6 +72,14 @@ export const DisablePrototypes = () => (
);
+export const NamedObject = () => (
+
+);
+
+export const NamedValue = () => (
+
+);
+
export const FontInheritance = () => (
diff --git a/components/ObjectInspector/src/stories/ObjectInspector.stories.tsx b/components/ObjectInspector/src/stories/ObjectInspector.stories.tsx
index a773afa..0cbf59c 100644
--- a/components/ObjectInspector/src/stories/ObjectInspector.stories.tsx
+++ b/components/ObjectInspector/src/stories/ObjectInspector.stories.tsx
@@ -1,5 +1,5 @@
import React from "react";
-import { boolean, number } from "@storybook/addon-knobs";
+import { boolean, number, text } from "@storybook/addon-knobs";
import { action } from "@storybook/addon-actions";
import { ObjectInspector } from "../ObjectInspector";
import notes from "../../README.md";
@@ -59,11 +59,20 @@ const data = {
},
};
+const nested = {
+ one: {
+ two: {
+ three: "test",
+ },
+ },
+};
+
export const Playground = () => {
const onSelect = action("onSelect");
return (
+### Named Root
+
+You can add a `name` to add a descriptive key to the root node,
+
+
+
### onSelect
You can use the `onSelect` prop to get the `@devtools-ds/object-parser` AST node for the currently selected part of the object.
diff --git a/packages/themes/src/AutoThemeProvider.tsx b/packages/themes/src/AutoThemeProvider.tsx
index 2d1e27d..484b593 100644
--- a/packages/themes/src/AutoThemeProvider.tsx
+++ b/packages/themes/src/AutoThemeProvider.tsx
@@ -3,14 +3,14 @@ import { all } from "./themes";
import { ThemeableElement, ThemeContext } from "./utils";
/** Determine if the current browser is FireFox */
-const isFirefox = () => {
+export const getBrowserTheme = () => {
if (window?.navigator?.userAgent) {
if (window.navigator.userAgent.toLowerCase().includes("firefox")) {
- return true;
+ return "firefox";
}
}
- return false;
+ return "chrome";
};
export interface AutoThemeProviderProps extends ThemeableElement<"div"> {
@@ -63,7 +63,7 @@ export const AutoThemeProvider = ({
}: AutoThemeProviderProps) => {
const isDark = useDarkMode();
const colorScheme = propsColorScheme || (isDark ? "dark" : "light");
- const theme = propsTheme || (isFirefox() ? "firefox" : "chrome");
+ const theme = propsTheme || getBrowserTheme();
const style = {
backgroundColor: all[theme][colorScheme].backgroundColor,
color: all[theme][colorScheme].textColor,