Skip to content

Commit

Permalink
perf: isolate reactivity and avoid calls
Browse files Browse the repository at this point in the history
  • Loading branch information
juanrgm committed Apr 20, 2022
1 parent de3f0a2 commit 6d3daf3
Show file tree
Hide file tree
Showing 27 changed files with 277 additions and 163 deletions.
8 changes: 8 additions & 0 deletions .changeset/three-sheep-hide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@suid/base": patch
"@suid/material": patch
"@suid/site": patch
"@suid/system": patch
---

Improve performance
12 changes: 8 additions & 4 deletions packages/base/src/BadgeUnstyled/useBadge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,12 @@ export default function useBadge(inProps: UseBadgeProps) {
}
});

return mergeProps(badge, () => ({
invisible: invisible(),
displayValue: displayValue(),
}));
return mergeProps(badge, {
get invisible() {
return invisible();
},
get displayValue() {
return displayValue();
},
});
}
12 changes: 8 additions & 4 deletions packages/base/src/ButtonUnstyled/ButtonUnstyled.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,14 @@ const ButtonUnstyled = $.component(function ButtonUnstyled({
props,
}) {
const button = useButton(props);
const ownerState = mergeProps(props, () => ({
active: button.active,
focusVisible: button.focusVisible,
}));
const ownerState = mergeProps(props, {
get active() {
return button.active;
},
get focusVisible() {
return button.focusVisible;
},
});

const ButtonRoot = () =>
otherProps.component ?? otherProps.components?.Root ?? "button";
Expand Down
16 changes: 6 additions & 10 deletions packages/base/src/utils/appendOwnerState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,12 @@ export default function appendOwnerState(
ownerState: object
) {
const mergedOwnerState = mergeProps(
() => existingProps().ownerState,
() => ownerState
() => existingProps().ownerState || {},
ownerState
);
return mergeProps(existingProps, () => {
if (isHostComponent(elementType())) {
return {};
} else {
return {
ownerState: mergedOwnerState,
};
}
return mergeProps(existingProps, {
get ownerState() {
if (!isHostComponent(elementType())) return mergedOwnerState;
},
});
}
8 changes: 5 additions & 3 deletions packages/material/src/Badge/Badge.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -241,9 +241,11 @@ const Badge = $.component(function Badge({ allProps, otherProps, props }) {
const invisible = useBadgeInvisibleMemo(allProps);
const badge = mergeProps(() => (invisible() ? prevProps : allProps));

const ownerState = mergeProps(allProps, () => ({
invisible: invisible(),
}));
const ownerState = mergeProps(allProps, {
get invisible() {
return invisible();
},
});

const classes = $.useClasses(ownerState);

Expand Down
8 changes: 5 additions & 3 deletions packages/material/src/Breadcrumbs/Breadcrumbs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,11 @@ const Breadcrumbs = $.component(function Breadcrumbs({
}) {
const [expanded, setExpanded] = createSignal(false);
const listElement = createElementRef();
const ownerState = mergeProps(allProps, () => ({
expanded: expanded(),
}));
const ownerState = mergeProps(allProps, {
get expanded() {
return expanded();
},
});

const handleClickExpand = () => {
setExpanded(true);
Expand Down
14 changes: 9 additions & 5 deletions packages/material/src/CardMedia/CardMedia.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,15 @@ const CardMedia = $.component(function CardMedia({
: {}
);

const ownerState = mergeProps(allProps, () => ({
isMediaComponent: isMediaComponent(),
isImageComponent:
IMAGE_COMPONENTS.indexOf(otherProps.component as any) !== -1,
}));
const ownerState = mergeProps(allProps, {
get isMediaComponent() {
return isMediaComponent();
},
get isImageComponent() {
return IMAGE_COMPONENTS.indexOf(otherProps.component as any) !== -1;
},
});

return (
<CardMediaRoot
role={!isMediaComponent() && props.image ? "img" : undefined}
Expand Down
5 changes: 1 addition & 4 deletions packages/material/src/Checkbox/Checkbox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,7 @@ const Checkbox = $.component(function Checkbox({ allProps, classes, props }) {
"size",
]);

const allClasses = mergeProps(
() => props.classes || {},
() => classes
);
const allClasses = mergeProps(() => props.classes || {}, classes);

return (
<SvgIconContext.Provider
Expand Down
17 changes: 8 additions & 9 deletions packages/material/src/FormControlLabel/FormControlLabel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -141,15 +141,14 @@ const FormControlLabel = $.component(function FormControlLabel({
states: ["error"],
});

const ownerState = mergeProps(
allProps,
() => ({
error: fcs.error,
}),
() => ({
disabled: childDisabled(),
})
);
const ownerState = mergeProps(allProps, {
get error() {
return fcs.error;
},
get disabled() {
return childDisabled();
},
});

const classes = $.useClasses(ownerState);

Expand Down
8 changes: 5 additions & 3 deletions packages/material/src/FormGroup/FormGroup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,11 @@ const FormGroup = $.component(function FormGroup({
states: ["error"],
});

const ownerState = mergeProps(allProps, () => ({
error: fcs.error,
}));
const ownerState = mergeProps(allProps, {
get error() {
return fcs.error;
},
});

return (
<FormGroupRoot
Expand Down
36 changes: 26 additions & 10 deletions packages/material/src/FormHelperText/FormHelperText.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -115,16 +115,32 @@ const FormHelperText = $.component(function FormHelperText({
],
});

const ownerState = mergeProps(allProps, () => ({
contained: fcs.variant === "filled" || fcs.variant === "outlined",
variant: fcs.variant,
size: fcs.size,
disabled: fcs.disabled,
error: fcs.error,
filled: fcs.filled,
focused: fcs.focused,
required: fcs.required,
}));
const ownerState = mergeProps(allProps, {
get contained() {
return fcs.variant === "filled" || fcs.variant === "outlined";
},
get variant() {
return fcs.variant;
},
get size() {
return fcs.size;
},
get disabled() {
return fcs.disabled;
},
get error() {
return fcs.error;
},
get filled() {
return fcs.filled;
},
get focused() {
return fcs.focused;
},
get required() {
return fcs.required;
},
});

const resolved = children(() => props.children);

Expand Down
28 changes: 20 additions & 8 deletions packages/material/src/FormLabel/FormLabel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,26 @@ const FormLabel = $.component(function FormLabel({
states: ["color", "required", "focused", "disabled", "error", "filled"],
});

const ownerState = mergeProps(allProps, () => ({
color: fcs.color || "primary",
disabled: fcs.disabled,
error: fcs.error,
filled: fcs.filled,
focused: fcs.focused,
required: fcs.required,
}));
const ownerState = mergeProps(allProps, {
get color() {
return fcs.color || "primary";
},
get disabled() {
return fcs.disabled;
},
get error() {
return fcs.error;
},
get filled() {
return fcs.filled;
},
get focused() {
return fcs.focused;
},
get required() {
return fcs.required;
},
});

const classes = $.useClasses(ownerState);

Expand Down
55 changes: 35 additions & 20 deletions packages/material/src/InputBase/InputBase.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -402,12 +402,11 @@ const InputBase = $.component(function InputBase({
],
});

const fcs = mergeProps(
() => partialFcs,
() => ({
focused: muiFormControl ? muiFormControl.focused : focused(),
})
);
const fcs = mergeProps(partialFcs, {
get focused() {
return muiFormControl ? muiFormControl.focused : focused();
},
});

// The blur won't fire when the disabled state is set on a focused input.
// We need to book keep the focused state manually.
Expand Down Expand Up @@ -485,33 +484,49 @@ const InputBase = $.component(function InputBase({
muiFormControl?.setAdornedStart(Boolean(props.startAdornment));
});

const ownerState = mergeProps(allProps, () => ({
color: fcs.color || "primary",
disabled: fcs.disabled,
error: fcs.error,
focused: fcs.focused,
formControl: muiFormControl,
hiddenLabel: fcs.hiddenLabel,
size: fcs.size,
}));
const ownerState = mergeProps(allProps, {
get color() {
return fcs.color || "primary";
},
get disabled() {
return fcs.disabled;
},
get error() {
return fcs.error;
},
get focused() {
return fcs.focused;
},
get formControl() {
return muiFormControl;
},
get hiddenLabel() {
return fcs.hiddenLabel;
},
get size() {
return fcs.size;
},
});

const classes = $.useClasses(ownerState);
const Root = () => props.components.Root || InputBaseRoot;
const rootProps = () => props.componentsProps.root || {};
const Input = () => props.components.Input || InputBaseComponent;

const rootOwnerState = mergeProps(
() => ownerState,
ownerState,
() => ((rootProps() as any)["ownerState"] || {}) as typeof ownerState
);
const inputOwnerState = mergeProps(
() => ownerState,
ownerState,
() => ((inputProps() as any)["ownerState"] || {}) as typeof ownerState
);

const renderSuffixProps = mergeProps(fcs, () => ({
startAdornment: props.startAdornment,
}));
const renderSuffixProps = mergeProps(fcs, {
get startAdornment() {
return props.startAdornment;
},
});

const suffix = createMemo(() => props.renderSuffix?.(renderSuffixProps));

Expand Down
24 changes: 17 additions & 7 deletions packages/material/src/InputLabel/InputLabel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -173,13 +173,23 @@ const InputLabel = $.component(function InputLabel({ allProps, props }) {
states: ["size", "variant", "required"],
});

const ownerState = mergeProps(allProps, () => ({
formControl: muiFormControl,
shrink: shrink(),
size: fcs.size,
variant: fcs.variant,
required: fcs.required,
}));
const ownerState = mergeProps(allProps, {
get formControl() {
return muiFormControl;
},
get shrink() {
return shrink();
},
get size() {
return fcs.size;
},
get variant() {
return fcs.variant;
},
get required() {
return fcs.required;
},
});

const classes = $.useClasses(ownerState);

Expand Down
8 changes: 5 additions & 3 deletions packages/material/src/Link/Link.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,11 @@ const LinkRoot = styled(Typography, {
const Link = $.component(function Link({ allProps, otherProps, props }) {
const visible = useIsFocusVisible();
const [focusVisible, setFocusVisible] = createSignal(false);
const ownerState = mergeProps(allProps, () => ({
focusVisible: focusVisible(),
}));
const ownerState = mergeProps(allProps, {
get focusVisible() {
return focusVisible();
},
});
const classes = $.useClasses(ownerState);

return (
Expand Down
Loading

0 comments on commit 6d3daf3

Please sign in to comment.