Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

withContext and withMultiContexts #464

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 100 additions & 1 deletion packages/context/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import { createContext, createComponent, useContext, JSX, Context, FlowComponent } from "solid-js";
import {
Accessor,
Context,
FlowComponent,
JSX,
createComponent,
createContext,
useContext,
} from "solid-js";
import type { ContextProviderComponent } from "solid-js/types/reactive/signal";

export type ContextProviderProps = {
Expand Down Expand Up @@ -119,3 +127,94 @@ export function MultiProvider<T extends readonly [unknown?, ...unknown[]]>(props
};
return fn(0);
}

/**
* A utility-function to provide context to components.
*
* @param children Accessor of Children
* @param context Context<T>
* @param value T
*
* @example
* ```tsx
* const NumberContext = createContext<number>
*
* const children = withContext(
* () => props.children,
* NumberContext,
* 1
* )
* ```
*/

export function withContext<T>(
children: Accessor<JSX.Element | JSX.Element[]>,
context: Context<T>,
value: T,
) {
let result: JSX.Element | JSX.Element[];

context.Provider({
value,
children: (() => {
result = children();
return "";
}) as any as JSX.Element,
});

return () => result;
Copy link
Contributor Author

@bigmistqke bigmistqke May 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The accessor is redundant. We can also return result directly.

}

/*

Type validation of the `values` array thanks to the amazing @otonashixav (https://github.com/otonashixav)

*/

/**
* A utility-function to provide multiple context to components.
*
* @param children Accessor of Children
* @param values Array of tuples of `[Context<T>, value T]`.
*
* @example
* ```tsx
* const NumberContext = createContext<number>
* const StringContext = createContext<string>
* const children = withContext(
* () => props.children,
* [
* [NumberContext, 1],
* [StringContext, "string"]
* ]
* )
* ```
*/

export function withMultiContexts<T extends readonly [unknown?, ...unknown[]]>(
children: Accessor<JSX.Element | JSX.Element[]>,
values: {
[K in keyof T]: readonly [Context<T[K]>, [T[K]][T extends unknown ? 0 : never]];
},
) {
let result: JSX.Element | JSX.Element[];

const fn = (index: number) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it be simpler to use values.reduce((children, [context, value]), children) => ...)

Copy link
Contributor Author

@bigmistqke bigmistqke May 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something like

(values as [Context<any>, any]).reduce((acc, [context, value], index) => {
    return () =>
      context.Provider({
        value,
        children: () => {
          if (index === 0) result = acc();
          else acc();
        },
      });
  }, children)()

?

playground

I don't use reduce myself that much, so this code is a bit less readable to me personally, but that's mb more of a skill issue on my part.

const [context, value] = values[index]!;
context.Provider({
value,
children: (() => {
if (index < values.length - 1) {
fn(index + 1);
} else {
result = children();
}
return "";
}) as any as JSX.Element,
});
};

fn(0);

return () => result;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The accessor is redundant. We can also return result directly.

}