Skip to content

Commit 2e44f06

Browse files
authored
fix: Custom onThemeChange fix (#2645)
## Description Provide a concise summary of the changes made in this pull request - ## Pull request type Check the appropriate box: - [ ] Review Fixes - [ ] Documentation Overhaul - [ ] Feature/Story - Link one or more Engineering Tickets * - [ ] A-Force - [ ] Error in documentation - [ ] Maintenance ## Documentation tickets Link to one or more documentation tickets: - ## Checklist From the below options, select the ones that are applicable: - [ ] Checked for Grammarly suggestions. - [ ] Adhered to the writing checklist. - [ ] Adhered to the media checklist. - [ ] Verified and updated cross-references or added redirect rules. - [ ] Tested the redirect rules on deploy preview. - [ ] Validated the modifications made to the content on the deploy preview. - [ ] Validated the CSS modifications on different screen sizes.
1 parent fcf852d commit 2e44f06

File tree

1 file changed

+31
-8
lines changed

1 file changed

+31
-8
lines changed

website/docs/reference/widgets/custom.md

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -352,16 +352,39 @@ appsmith.onReady(() => {
352352

353353
<dd>
354354

355-
Subscribe to theme changes and execute a callback function.
355+
The `onThemeChange` function allows theme changes from the application to be applied to a Custom widget. It triggers a callback whenever the theme changes, allowing your widget to automatically reflect the updated design and style. The following theme properties are accessible:
356356

357-
```js
358-
// Set the primaryColor of your component using a function.
359-
const unlisten = appsmith.onThemeChange((theme, oldTheme) => {
360-
setPrimaryColor(theme.primaryColor);
361-
});
357+
- `primaryColor`: Represents the primary color of the application theme. For example, `#FF5733`.
358+
- `backgroundColor`: Represents the background color of the application theme. For example, `#f0f0f0`.
359+
- `borderRadius`: Represents the border radius for rounded corners in the theme. For example, `0px` or `1.5rem`.
360+
- `borderShadow`: Represents the shadow applied to borders in the theme. For example, `0px 4px 6px rgba(0, 0, 0, 0.1)`.
362361

363-
// Unsubscribe when no longer interested in updates.
364-
unlisten();
362+
363+
If you no longer need to listen for theme changes, you can unsubscribe by calling the function as a return statement to `onThemeChange` (e.g., `unlisten()`).
364+
365+
*Example:* If you want to create a button that adapts to the application's theme properties, you can use the `onThemeChange` function to dynamically apply styles like primary color, background color, border radius, and shadow.
366+
367+
```js
368+
function ThemedButton() {
369+
const [themeStyles, setThemeStyles] = React.useState({});
370+
371+
React.useEffect(() => {
372+
// Listen for theme changes and update button styles
373+
const unlisten = appsmith.onThemeChange((theme) => {
374+
setThemeStyles({
375+
color: theme.primaryColor,
376+
backgroundColor: theme.backgroundColor,
377+
borderRadius: theme.borderRadius,
378+
boxShadow: theme.borderShadow,
379+
});
380+
});
381+
return () => unlisten(); // Clean up listener on unmount
382+
}, []);
383+
384+
return (
385+
<Button style={themeStyles}>Next</Button>
386+
);
387+
}
365388
```
366389

367390
</dd>

0 commit comments

Comments
 (0)