Skip to content

Commit

Permalink
convert memo to selector
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Kruining committed Feb 17, 2025
1 parent 210d8e4 commit 39da4f6
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/components/tabs.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Accessor, children, createContext, createEffect, createMemo, createSignal, For, ParentComponent, Setter, Show, useContext } from "solid-js";
import { Accessor, children, createContext, createEffect, createMemo, createSelector, createSignal, For, ParentComponent, Setter, Show, useContext } from "solid-js";
import { Command, CommandType, noop, useCommands } from "~/features/command";
import { AiOutlineClose } from "solid-icons/ai";
import css from "./tabs.module.css";

type CloseTabCommandType = CommandType<(id: string) => any>;
interface TabsContextType {
activate(id: string | undefined): void;
isActive(id: string): Accessor<boolean>;
isActive(id: string): boolean;
readonly onClose: Accessor<CloseTabCommandType | undefined>
}

Expand All @@ -24,6 +24,7 @@ const useTabs = () => {

export const Tabs: ParentComponent<{ class?: string, active?: string, setActive?: Setter<string | undefined>, onClose?: CloseTabCommandType }> = (props) => {
const [active, setActive] = createSignal<string | undefined>(props.active);
const isActive = createSelector<string | undefined, string>(active, (id, active) => id === active);

createEffect(() => {
props.setActive?.(active());
Expand All @@ -34,9 +35,11 @@ export const Tabs: ParentComponent<{ class?: string, active?: string, setActive?
setActive(id);
},

isActive(id: string) {
return createMemo(() => active() === id);
},
isActive,

// isActive(id: string) {
// return createMemo(() => active() === id);
// },

onClose: createMemo(() => props.onClose),
};
Expand Down Expand Up @@ -91,15 +94,14 @@ const _Tabs: ParentComponent<{ class?: string, active: string | undefined, onClo
export const Tab: ParentComponent<{ id: string, label: string, closable?: boolean }> = (props) => {
const context = useTabs();
const resolved = children(() => props.children);
const isActive = context.isActive(props.id);

return <div
id={props.id}
class={css.tab}
data-tab-label={props.label}
data-tab-closable={props.closable}
>
<Show when={isActive()}>
<Show when={context.isActive(props.id)}>
<Command.Context for={context.onClose() ?? noop} with={[props.id]}>{resolved()}</Command.Context>
</Show>
</div>;
Expand Down

0 comments on commit 39da4f6

Please sign in to comment.