Skip to content

Commit

Permalink
perf: reduce calls to propDefaults
Browse files Browse the repository at this point in the history
  • Loading branch information
juanrgm committed Apr 27, 2022
1 parent 938d054 commit 70a0607
Show file tree
Hide file tree
Showing 12 changed files with 94 additions and 59 deletions.
6 changes: 6 additions & 0 deletions .changeset/fresh-files-play.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@suid/base": patch
"@suid/material": patch
---

Increase performance reducing calls
13 changes: 6 additions & 7 deletions packages/base/src/createComponentFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,12 @@ function createComponentFactory<
const theme = useTheme();
const set = (v: any) => v;
const inProps = mergeProps(
() =>
options.propDefaults?.({
set,
theme,
inProps: input.inProps ?? (inProps as any),
}) ?? {},
() => theme.components?.[options.name]?.defaultProps ?? {},
options.propDefaults?.({
set,
theme,
inProps: input.inProps,
}) || {},
() => theme.components?.[options.name]?.defaultProps || {},
input.inProps
) as InProps;
const [props, otherProps] = splitProps(inProps, options.selfPropNames);
Expand Down
28 changes: 21 additions & 7 deletions packages/material/src/Button/Button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,28 @@ const $ = createComponentFactory<ButtonTypeMap>()({
propDefaults: ({ set }) => {
const contextProps = useContext(ButtonGroupContext);
return set({
color: contextProps.color ?? "primary",
get color() {
return contextProps.color ?? "primary";
},
component: "button",
disabled: contextProps.disabled ?? false,
disableElevation: contextProps.disableElevation ?? false,
disableFocusRipple: contextProps.disableFocusRipple ?? false,
fullWidth: contextProps.fullWidth ?? false,
size: contextProps.size ?? "medium",
variant: contextProps.variant ?? "text",
get disabled() {
return contextProps.disabled ?? false;
},
get disableElevation() {
return contextProps.disableElevation ?? false;
},
get disableFocusRipple() {
return contextProps.disableFocusRipple ?? false;
},
get fullWidth() {
return contextProps.fullWidth ?? false;
},
get size() {
return contextProps.size ?? "medium";
},
get variant() {
return contextProps.variant ?? "text";
},
});
},
utilityClass: getButtonUtilityClass,
Expand Down
8 changes: 6 additions & 2 deletions packages/material/src/Divider/Divider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,15 @@ const $ = createComponentFactory<DividerTypeMap>()({
propDefaults: ({ set, inProps }) =>
set({
absolute: false,
component: inProps.children ? "div" : "hr",
get component() {
return inProps.children ? "div" : "hr";
},
flexItem: false,
light: false,
orientation: "horizontal",
role: inProps.component !== "hr" ? "separator" : undefined,
get role() {
return inProps.component !== "hr" ? "separator" : undefined;
},
textAlign: "center",
variant: "fullWidth",
}),
Expand Down
16 changes: 8 additions & 8 deletions packages/material/src/Drawer/Drawer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,23 +49,23 @@ const $ = createComponentFactory<DrawerTypeMap>()({
`paperAnchorDocked${capitalize(ownerState.anchor)}`,
],
}),
propDefaults: ({ set }) => {
const theme = useTheme();
return set({
propDefaults: ({ set, theme }) =>
set({
component: "div",
anchor: "left",
elevation: 16,
hideBackdrop: false,
ModalProps: {},
open: false,
PaperProps: {},
transitionDuration: {
enter: theme.transitions.duration.enteringScreen,
exit: theme.transitions.duration.leavingScreen,
get transitionDuration() {
return {
enter: theme.transitions.duration.enteringScreen,
exit: theme.transitions.duration.leavingScreen,
};
},
variant: "temporary",
});
},
}),
});

const DrawerRoot = styled(Modal, {
Expand Down
16 changes: 8 additions & 8 deletions packages/material/src/Fade/Fade.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ import { children, createMemo, useContext } from "solid-js";
const $ = createComponentFactory<FadeTypeMap>()({
name: "MuiFader",
selfPropNames: ["appear", "children", "easing", "in", "timeout"],
propDefaults: ({ set }) => {
const theme = useTheme();
return set({
propDefaults: ({ set, theme }) =>
set({
appear: true,
timeout: {
enter: theme.transitions.duration.enteringScreen,
exit: theme.transitions.duration.leavingScreen,
get timeout() {
return {
enter: theme.transitions.duration.enteringScreen,
exit: theme.transitions.duration.leavingScreen,
};
},
});
},
}),
});

export const fadeSelfPropNames = $.selfPropNames;
Expand Down
8 changes: 6 additions & 2 deletions packages/material/src/Grid/Grid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,12 @@ const $ = createComponentFactory<GridTypeMap>()({
xl: false,
xs: false,
zeroMinWidth: false,
rowSpacing: inProps.rowSpacing ?? inProps.spacing ?? 0,
columnSpacing: inProps.columnSpacing ?? inProps.spacing ?? 0,
get rowSpacing() {
return inProps.rowSpacing ?? inProps.spacing ?? 0;
},
get columnSpacing() {
return inProps.columnSpacing ?? inProps.spacing ?? 0;
},
}),
utilityClass: getGridUtilityClass,
slotClasses: (o) => ({
Expand Down
13 changes: 8 additions & 5 deletions packages/material/src/List/List.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import styled from "../styles/styled";
import ListContext, { createListContextMutable } from "./ListContext";
import ListContext from "./ListContext";
import { ListTypeMap } from "./ListProps";
import { getListUtilityClass } from "./listClasses";
import createComponentFactory from "@suid/base/createComponentFactory";
Expand Down Expand Up @@ -75,11 +75,14 @@ const List = $.component(function List({
otherProps,
props,
}) {
const context = createListContextMutable({
dense: props.dense,
});
return (
<ListContext.Provider value={context}>
<ListContext.Provider
value={{
get dense() {
return props.dense;
},
}}
>
<ListRoot
{...otherProps}
className={clsx(classes.root, otherProps.className)}
Expand Down
5 changes: 0 additions & 5 deletions packages/material/src/List/ListContext.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { createContext, useContext } from "solid-js";
import { createMutable } from "solid-js/store";

export interface ListContext {
dense: boolean;
Expand All @@ -15,8 +14,4 @@ export function useListContext() {
return useContext(ListContext);
}

export function createListContextMutable(data: ListContext) {
return createMutable(data);
}

export default ListContext;
24 changes: 13 additions & 11 deletions packages/material/src/Slide/Slide.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,23 @@ const $ = createComponentFactory<SlideTypeMap>()({
"in",
"timeout",
],
propDefaults: ({ set }) => {
const theme = useTheme();
return set({
propDefaults: ({ set, theme }) =>
set({
appear: true,
direction: "down",
easing: {
enter: theme.transitions.easing.easeOut,
exit: theme.transitions.easing.sharp,
get easing() {
return {
enter: theme.transitions.easing.easeOut,
exit: theme.transitions.easing.sharp,
};
},
timeout: {
enter: theme.transitions.duration.enteringScreen,
exit: theme.transitions.duration.leavingScreen,
get timeout() {
return {
enter: theme.transitions.duration.enteringScreen,
exit: theme.transitions.duration.leavingScreen,
};
},
});
},
}),
});

type Direction = "left" | "right" | "up" | "down";
Expand Down
4 changes: 3 additions & 1 deletion packages/material/src/SvgIcon/SvgIcon.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ const $ = createComponentFactory<SvgIconTypeMap>()({
return set({
component: "svg",
color: "inherit",
fontSize: context?.fontSize ?? "medium",
get fontSize() {
return context?.fontSize ?? "medium";
},
inheritViewBox: false,
viewBox: "0 0 24 24",
});
Expand Down
12 changes: 9 additions & 3 deletions packages/material/src/ToggleButton/ToggleButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,17 @@ const $ = createComponentFactory<ToggleButtonTypeMap>()({
propDefaults: ({ set }) => {
const context = useContext(ToggleButtonGroupContext);
return set({
color: context.color ?? "standard",
disabled: context.disabled ?? false,
get color() {
return context.color ?? "standard";
},
get disabled() {
return context.disabled ?? false;
},
disableFocusRipple: false,
fullWidth: false,
size: context.size ?? "medium",
get size() {
return context.size ?? "medium";
},
});
},
autoCallUseClasses: false,
Expand Down

0 comments on commit 70a0607

Please sign in to comment.