Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

Update toggle, add tests

Nate Butler61535ed41f62
diff --git a/styles/src/element/index.ts b/styles/src/element/index.ts
index ad58b89e0e8..b1e3cfe415f 100644
--- a/styles/src/element/index.ts
+++ b/styles/src/element/index.ts
@@ -1,3 +1,4 @@
import { interactive } from "./interactive"
+import { toggleable } from "./toggle"
-export { interactive }
+export { interactive, toggleable }
diff --git a/styles/src/element/toggle.test.ts b/styles/src/element/toggle.test.ts
new file mode 100644
index 00000000000..8018ce10393
--- /dev/null
+++ b/styles/src/element/toggle.test.ts
@@ -0,0 +1,52 @@
+import {
+ NO_ACTIVE_ERROR,
+ NO_INACTIVE_OR_BASE_ERROR,
+ toggleable,
+} from "./toggle"
+import { describe, it, expect } from "vitest"
+
+describe("toggleable", () => {
+ it("creates a Toggleable<Element> with base properties and states", () => {
+ const result = toggleable({
+ base: { background: "#000000", color: "#CCCCCC" },
+ state: {
+ active: { color: "#FFFFFF" },
+ },
+ })
+
+ expect(result).toEqual({
+ inactive: { background: "#000000", color: "#CCCCCC" },
+ active: { background: "#000000", color: "#FFFFFF" },
+ })
+ })
+
+ it("creates a Toggleable<Element> with no base properties", () => {
+ const result = toggleable({
+ state: {
+ inactive: { background: "#000000", color: "#CCCCCC" },
+ active: { background: "#000000", color: "#FFFFFF" },
+ },
+ })
+
+ expect(result).toEqual({
+ inactive: { background: "#000000", color: "#CCCCCC" },
+ active: { background: "#000000", color: "#FFFFFF" },
+ })
+ })
+
+ it("throws error when both inactive and base are missing", () => {
+ const state = {
+ active: { background: "#000000", color: "#FFFFFF" },
+ }
+
+ expect(() => toggleable({ state })).toThrow(NO_INACTIVE_OR_BASE_ERROR)
+ })
+
+ it("throws error when no active state is present", () => {
+ const state = {
+ inactive: { background: "#000000", color: "#CCCCCC" },
+ }
+
+ expect(() => toggleable({ state })).toThrow(NO_ACTIVE_ERROR)
+ })
+})
diff --git a/styles/src/element/toggle.ts b/styles/src/element/toggle.ts
index 2b6858e15be..84390810c0d 100644
--- a/styles/src/element/toggle.ts
+++ b/styles/src/element/toggle.ts
@@ -4,9 +4,9 @@ type ToggleState = "inactive" | "active"
type Toggleable<T> = Record<ToggleState, T>
-const NO_INACTIVE_OR_BASE_ERROR =
+export const NO_INACTIVE_OR_BASE_ERROR =
"A toggleable object must have an inactive state, or a base property."
-const NO_ACTIVE_ERROR = "A toggleable object must have an active state."
+export const NO_ACTIVE_ERROR = "A toggleable object must have an active state."
interface ToggleableProps<T> {
base?: T
diff --git a/styles/src/styleTree/commandPalette.ts b/styles/src/styleTree/commandPalette.ts
index ae0aae2f81a..59e12945db8 100644
--- a/styles/src/styleTree/commandPalette.ts
+++ b/styles/src/styleTree/commandPalette.ts
@@ -1,8 +1,7 @@
import { ColorScheme } from "../theme/colorScheme"
import { withOpacity } from "../theme/color"
import { text, background } from "./components"
-import { toggleable } from "./toggle"
-import { interactive } from "../element"
+import { interactive, toggleable } from "../element"
export default function commandPalette(colorScheme: ColorScheme) {
let layer = colorScheme.highest
diff --git a/styles/src/styleTree/contactList.ts b/styles/src/styleTree/contactList.ts
index 5c6068408ec..2ac17a4584c 100644
--- a/styles/src/styleTree/contactList.ts
+++ b/styles/src/styleTree/contactList.ts
@@ -1,7 +1,6 @@
import { ColorScheme } from "../theme/colorScheme"
import { background, border, borderColor, foreground, text } from "./components"
-import { toggleable } from "./toggle"
-import { interactive } from "../element"
+import { interactive, toggleable } from "../element"
export default function contactsPanel(colorScheme: ColorScheme) {
const nameMargin = 8
const sidePadding = 12
diff --git a/styles/src/styleTree/contextMenu.ts b/styles/src/styleTree/contextMenu.ts
index 6f5eb536405..90ba0ce5b53 100644
--- a/styles/src/styleTree/contextMenu.ts
+++ b/styles/src/styleTree/contextMenu.ts
@@ -1,7 +1,6 @@
import { ColorScheme } from "../theme/colorScheme"
import { background, border, borderColor, text } from "./components"
-import { interactive } from "../element"
-import { toggleable } from "./toggle"
+import { interactive, toggleable } from "../element"
export default function contextMenu(colorScheme: ColorScheme) {
let layer = colorScheme.middle
diff --git a/styles/src/styleTree/editor.ts b/styles/src/styleTree/editor.ts
index 9dec64b0ced..e029a724a39 100644
--- a/styles/src/styleTree/editor.ts
+++ b/styles/src/styleTree/editor.ts
@@ -4,8 +4,7 @@ import { background, border, borderColor, foreground, text } from "./components"
import hoverPopover from "./hoverPopover"
import { buildSyntax } from "../theme/syntax"
-import { interactive } from "../element"
-import { toggleable } from "./toggle"
+import { interactive, toggleable } from "../element"
export default function editor(colorScheme: ColorScheme) {
const { isLight } = colorScheme
diff --git a/styles/src/styleTree/picker.ts b/styles/src/styleTree/picker.ts
index 7730395ec96..12fecfa6f89 100644
--- a/styles/src/styleTree/picker.ts
+++ b/styles/src/styleTree/picker.ts
@@ -1,8 +1,7 @@
import { ColorScheme } from "../theme/colorScheme"
import { withOpacity } from "../theme/color"
import { background, border, text } from "./components"
-import { toggleable } from "./toggle"
-import { interactive } from "../element"
+import { interactive, toggleable } from "../element"
export default function picker(colorScheme: ColorScheme): any {
let layer = colorScheme.lowest
diff --git a/styles/src/styleTree/projectPanel.ts b/styles/src/styleTree/projectPanel.ts
index 23df2fbf58f..5f283e551dc 100644
--- a/styles/src/styleTree/projectPanel.ts
+++ b/styles/src/styleTree/projectPanel.ts
@@ -1,8 +1,7 @@
import { ColorScheme } from "../theme/colorScheme"
import { withOpacity } from "../theme/color"
import { background, border, foreground, text } from "./components"
-import { interactive } from "../element"
-import { toggleable } from "./toggle"
+import { interactive, toggleable } from "../element"
export default function projectPanel(colorScheme: ColorScheme) {
const { isLight } = colorScheme
diff --git a/styles/src/styleTree/search.ts b/styles/src/styleTree/search.ts
index a95296971c1..4b0869f10e6 100644
--- a/styles/src/styleTree/search.ts
+++ b/styles/src/styleTree/search.ts
@@ -1,8 +1,7 @@
import { ColorScheme } from "../theme/colorScheme"
import { withOpacity } from "../theme/color"
import { background, border, foreground, text } from "./components"
-import { interactive } from "../element"
-import { toggleable } from "./toggle"
+import { interactive, toggleable } from "../element"
export default function search(colorScheme: ColorScheme) {
let layer = colorScheme.highest
diff --git a/styles/src/styleTree/statusBar.ts b/styles/src/styleTree/statusBar.ts
index 8a91b7eaac4..d6d7eda732c 100644
--- a/styles/src/styleTree/statusBar.ts
+++ b/styles/src/styleTree/statusBar.ts
@@ -1,7 +1,6 @@
import { ColorScheme } from "../theme/colorScheme"
import { background, border, foreground, text } from "./components"
-import { interactive } from "../element"
-import { toggleable } from "./toggle"
+import { interactive, toggleable } from "../element"
export default function statusBar(colorScheme: ColorScheme) {
let layer = colorScheme.lowest
diff --git a/styles/src/styleTree/tabBar.ts b/styles/src/styleTree/tabBar.ts
index c2b5de02159..e00f0b8b83c 100644
--- a/styles/src/styleTree/tabBar.ts
+++ b/styles/src/styleTree/tabBar.ts
@@ -1,8 +1,7 @@
import { ColorScheme } from "../theme/colorScheme"
import { withOpacity } from "../theme/color"
import { text, border, background, foreground } from "./components"
-import { toggleable } from "./toggle"
-import { interactive } from "../element"
+import { interactive, toggleable } from "../element"
export default function tabBar(colorScheme: ColorScheme) {
const height = 32
diff --git a/styles/src/styleTree/toolbarDropdownMenu.ts b/styles/src/styleTree/toolbarDropdownMenu.ts
index 1f47c85f4e7..3ce7ca7d7cd 100644
--- a/styles/src/styleTree/toolbarDropdownMenu.ts
+++ b/styles/src/styleTree/toolbarDropdownMenu.ts
@@ -1,7 +1,6 @@
import { ColorScheme } from "../theme/colorScheme"
import { background, border, text } from "./components"
-import { interactive } from "../element"
-import { toggleable } from "./toggle"
+import { interactive, toggleable } from "../element"
export default function dropdownMenu(colorScheme: ColorScheme) {
let layer = colorScheme.middle
diff --git a/styles/src/styleTree/workspace.ts b/styles/src/styleTree/workspace.ts
index 79e7830aec8..a2a21eaff4a 100644
--- a/styles/src/styleTree/workspace.ts
+++ b/styles/src/styleTree/workspace.ts
@@ -1,6 +1,6 @@
import { ColorScheme } from "../theme/colorScheme"
import { withOpacity } from "../theme/color"
-import { toggleable } from "./toggle"
+import { toggleable } from "../element"
import {
background,
border,