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

fix: fix getting listbox theme #1156

Merged
merged 8 commits into from
Mar 17, 2023
Merged
Show file tree
Hide file tree
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
9 changes: 6 additions & 3 deletions apis/nucleus/src/app-theme.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,18 @@ const timed = (t, v) =>

const LOAD_THEME_TIMEOUT = 5000;

export default function appTheme({ themes = [], root } = {}) {
export default function appTheme({ themes = [], loadTheme, root } = {}) {
const wrappedTheme = themeFn();

const setTheme = async (themeId) => {
const found = themes.filter((t) => t.id === themeId)[0];
let found = themes.filter((t) => t.id === themeId)[0];
let muiTheme = themeId === 'dark' ? 'dark' : 'light';
if (!found && loadTheme) {
found = { load: loadTheme };
}
if (found && found.load) {
try {
const raw = await Promise.race([found.load(), timed(LOAD_THEME_TIMEOUT, { __timedOut: true })]);
const raw = await Promise.race([found.load(themeId), timed(LOAD_THEME_TIMEOUT, { __timedOut: true })]);
if (raw.__timedOut) {
if (__NEBULA_DEV__) {
console.warn(`Timeout when loading theme '${themeId}'`); // eslint-disable-line no-console
Expand Down
1 change: 1 addition & 0 deletions apis/nucleus/src/components/listbox/ListBoxInline.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ const Title = styled(Typography)(({ theme }) => ({
color: theme.listBox?.title?.main?.color,
fontSize: theme.listBox?.title?.main?.fontSize,
fontFamily: theme.listBox?.title?.main?.fontFamily,
fontWeight: theme.listBox?.title?.main?.fontWeight || 'bold',
}));

function ListBoxInline({ options, layout }) {
Expand Down
17 changes: 9 additions & 8 deletions apis/nucleus/src/components/listbox/assets/addListboxTheme.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
const addListboxTheme = (themeApi) => {
const getListboxStyle = (prop) => themeApi.getStyle('object', '', `listBox.${prop}`);
const getListboxStyle = (path, prop) => themeApi.getStyle('object.listBox', path, prop);

return {
backgroundColor: getListboxStyle('backgroundColor'),
backgroundColor: getListboxStyle('', 'backgroundColor'),
title: {
main: {
color: getListboxStyle('title.main.color'),
fontSize: getListboxStyle('title.main.fontSize'),
fontFamily: getListboxStyle('title.main.fontFamily'),
color: getListboxStyle('title.main', 'color'),
fontSize: getListboxStyle('title.main', 'fontSize'),
fontFamily: getListboxStyle('title.main', 'fontFamily'),
fontWeight: getListboxStyle('title.main', 'fontWeight'),
},
},
content: {
color: getListboxStyle('content.color'),
fontSize: getListboxStyle('content.fontSize'),
fontFamily: getListboxStyle('content.fontFamily'),
color: getListboxStyle('content', 'color'),
fontSize: getListboxStyle('content', 'fontSize'),
fontFamily: getListboxStyle('content', 'fontFamily'),
},
};
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ export default function ListBoxSearch({
sx={[
{
border: 'none',
fontSize: 14,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Added this as well to the search field isn't huge compared to the rest

borderRadius: 0,
backgroundColor: 'transparent',
'& fieldset': {
Expand Down
2 changes: 2 additions & 0 deletions apis/nucleus/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ const mergeArray = (a1 = [], a2 = []) =>
const mergeConfigs = (base, c) => ({
context: mergeObj(base.context, c.context),
load: c.load || base.load,
loadTheme: c.loadTheme || base.loadTheme,
snapshot: {
...(c.snapshot || base.snapshot),
},
Expand Down Expand Up @@ -175,6 +176,7 @@ function nuked(configuration = {}) {

const appTheme = appThemeFn({
themes: configuration.themes,
loadTheme: configuration.loadTheme,
root,
});
currentContext.themeApi = appTheme.externalAPI;
Expand Down
18 changes: 14 additions & 4 deletions apis/theme/src/__tests__/set-theme.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import create from '../set-theme';
import base from '../themes/base.json';
import light from '../themes/light.json';
import dark from '../themes/dark.json';
import baseIn from '../themes/base_inherit.json';

jest.mock('extend');
jest.mock('../themes/base.json', () => ({
Expand Down Expand Up @@ -36,17 +37,26 @@ describe('set theme', () => {
palettes: {},
});

create({}, resolveMock);
expect(extendMock.mock.calls[0]).toEqual([true, {}, base, light]);
create({ _inherit: false }, resolveMock);
expect(extendMock.mock.calls[0]).toEqual([true, {}, base, light, {}]);
});

test('should extend from dark theme when type is dark', () => {
extendMock.mockReturnValue({
palettes: {},
});

create({ type: 'dark' }, resolveMock);
expect(extendMock.mock.calls[0]).toEqual([true, {}, base, dark]);
create({ type: 'dark', _inherit: false }, resolveMock);
expect(extendMock.mock.calls[0]).toEqual([true, {}, base, dark, {}]);
});

test('should extend and add base inheritance', () => {
extendMock.mockReturnValue({
palettes: {},
});

create({}, resolveMock);
expect(extendMock.mock.calls[0]).toEqual([true, {}, base, light, baseIn]);
});

test('should not extend scales and palette arrays', () => {
Expand Down
8 changes: 7 additions & 1 deletion apis/theme/src/set-theme.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
/* eslint no-underscore-dangle:0 */
import extend from 'extend';

import baseRawJSON from './themes/base.json';
import lightRawJSON from './themes/light.json';
import darkRawJSON from './themes/dark.json';
import baseInheritRawJSON from './themes/base_inherit.json';

export default function setTheme(t, resolve) {
const colorRawJSON = t.type === 'dark' ? darkRawJSON : lightRawJSON;
const root = extend(true, {}, baseRawJSON, colorRawJSON);
let baseInherit = baseInheritRawJSON;
if (t._inherit === false || t._inherit === 'false') {
baseInherit = {};
}
const root = extend(true, {}, baseRawJSON, colorRawJSON, baseInherit);
// avoid merging known array objects as it could cause issues if they are of different types (pyramid vs class) or length
const rawThemeJSON = extend(true, {}, root, { scales: null, palettes: { data: null, ui: null } }, t);
if (!rawThemeJSON.palettes.data || !rawThemeJSON.palettes.data.length) {
Expand Down
18 changes: 18 additions & 0 deletions apis/theme/src/themes/base_inherit.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"object": {
"listBox": {
"backgroundColor": "#ffffff",
"title": {
"main": {
"color": "#404040",
"fontSize": "14px",
"fontWeight": "bold"
}
},
"content": {
"color": "#404040",
"fontSize": "12px"
}
}
}
}
2 changes: 1 addition & 1 deletion test/rendering/listbox/listbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
sc = { toolbar: false };
break;
case 'longTitle':
sc = { title: 'Alpha long title' };
sc = { title: 'Alpha long title that needs more words' };
break;
default:
throw new Error('Invalid test scenario', s);
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.