Skip to content

Commit d0c811d

Browse files
feat(ThemeProvider): add global css classes (#400)
1 parent 84a3f90 commit d0c811d

File tree

12 files changed

+70
-52
lines changed

12 files changed

+70
-52
lines changed

docs/welcome/Styling.stories.mdx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { ThemeProvider } from '@ui5/webcomponents-react/lib/ThemeProvider';
88
# How can I style my Web Components?
99
[Open Github Project](https://github.com/SAP/ui5-webcomponents-react)
1010

11-
## Change styles for exisiting components
11+
## Change styles for existing components
1212
You can easily change the appearance of the Web Components by using [CSS Variables](https://www.w3schools.com/Css/css3_variables.asp).
1313
Per default, we are injecting the Fiori 3 theme parameters as CSS Variables into the `document head`.
1414
If you want to change e.g. the color of all texts, you can do that by creating another `style` element with the following content:
@@ -23,6 +23,14 @@ You can change CSS Variables on any level - in the head, or on every single elem
2323
A full list of all supported CSS Variables can be found [here](https://github.com/SAP/ui5-webcomponents-react/blob/master/packages/base/src/styling/sap_fiori_3.ts)
2424
or in the [SAPUI5 Theming Parameters Toolbox](https://sapui5.hana.ondemand.com/test-resources/sap/m/demokit/theming/webapp/index.html).
2525
26+
## Reuse global style classes
27+
The `ThemeProvider` component is offering global css classes that can be reused in your applications code to help you to achieve a Fiori look-and-feel.
28+
All globally available style classes are tracked in the `GlobalStyleClasses` enum which can be imported from `import { GlobalStyleClasses } from "@ui5/webcomponents-react/lib/GlobalStyleClasses";`.
29+
30+
|CSS Class|Description|
31+
|---------|-----------|
32+
|`sapScrollBar`|If added to an container that supports scrolling, the scrollbar will be styled according to the Fiori guidelines. |
33+
2634
## Style your own components
2735
It's quite likely that you have to create some custom components when you are building an app.
2836
In order to reuse our central styling approach, you can easily hook into our theming by using [`react-jss`](https://cssinjs.org/react-jss/?v=v10.0.0).

packages/main/src/components/AnalyticalTable/AnayticalTable.jss.ts

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,22 +39,7 @@ const styles = {
3939
zIndex: 0,
4040
backgroundColor: ThemingParameters.sapList_Background,
4141
overflowX: 'hidden !important',
42-
overflowY: 'auto !important',
43-
'&::-webkit-scrollbar': {
44-
backgroundColor: ThemingParameters.sapScrollBar_TrackColor,
45-
width: ThemingParameters.sapScrollBar_Dimension
46-
},
47-
'&::-webkit-scrollbar-thumb': {
48-
backgroundColor: ThemingParameters.sapScrollBar_FaceColor,
49-
width: ThemingParameters.sapScrollBar_Dimension,
50-
borderRadius: '0.25rem',
51-
'&:hover': {
52-
backgroundColor: ThemingParameters.sapScrollBar_Hover_FaceColor
53-
}
54-
},
55-
'&::-webkit-scrollbar-corner': {
56-
backgroundColor: ThemingParameters.sapScrollBar_TrackColor
57-
}
42+
overflowY: 'auto !important'
5843
},
5944
alternateRowColor: {
6045
backgroundColor: ThemingParameters.sapList_HeaderBackground

packages/main/src/components/AnalyticalTable/__snapshots__/AnalyticalTable.test.tsx.snap

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ exports[`AnalyticalTable Alternate Row Color 1`] = `
279279
</div>
280280
</header>
281281
<div
282-
class="AnalyticalTable-tbody-0"
282+
class="AnalyticalTable-tbody-0 sapScrollBar"
283283
style="position: relative; height: 220px; width: 600px; overflow: auto; will-change: transform; direction: ltr;"
284284
>
285285
<div
@@ -770,7 +770,7 @@ exports[`AnalyticalTable Loading - Loader 1`] = `
770770
</div>
771771
</header>
772772
<div
773-
class="AnalyticalTable-tbody-0"
773+
class="AnalyticalTable-tbody-0 sapScrollBar"
774774
style="position: relative; height: 220px; width: 600px; overflow: auto; will-change: transform; direction: ltr;"
775775
>
776776
<div
@@ -1884,7 +1884,7 @@ exports[`AnalyticalTable Tree Table 1`] = `
18841884
</div>
18851885
</header>
18861886
<div
1887-
class="AnalyticalTable-tbody-0 AnalyticalTable-selectable-0"
1887+
class="AnalyticalTable-tbody-0 sapScrollBar AnalyticalTable-selectable-0"
18881888
style="position: relative; height: 220px; width: 636px; overflow: auto; will-change: transform; direction: ltr;"
18891889
>
18901890
<div
@@ -2437,7 +2437,7 @@ exports[`AnalyticalTable custom row height 1`] = `
24372437
</div>
24382438
</header>
24392439
<div
2440-
class="AnalyticalTable-tbody-0"
2440+
class="AnalyticalTable-tbody-0 sapScrollBar"
24412441
style="position: relative; height: 300px; width: 600px; overflow: auto; will-change: transform; direction: ltr;"
24422442
>
24432443
<div
@@ -3217,7 +3217,7 @@ exports[`AnalyticalTable test Asc desc 1`] = `
32173217
</div>
32183218
</header>
32193219
<div
3220-
class="AnalyticalTable-tbody-0"
3220+
class="AnalyticalTable-tbody-0 sapScrollBar"
32213221
style="position: relative; height: 220px; width: 600px; overflow: auto; will-change: transform; direction: ltr;"
32223222
>
32233223
<div
@@ -3708,7 +3708,7 @@ exports[`AnalyticalTable test Asc desc 2`] = `
37083708
</div>
37093709
</header>
37103710
<div
3711-
class="AnalyticalTable-tbody-0"
3711+
class="AnalyticalTable-tbody-0 sapScrollBar"
37123712
style="position: relative; height: 220px; width: 600px; overflow: auto; will-change: transform; direction: ltr;"
37133713
>
37143714
<div
@@ -4199,7 +4199,7 @@ exports[`AnalyticalTable test Asc desc 3`] = `
41994199
</div>
42004200
</header>
42014201
<div
4202-
class="AnalyticalTable-tbody-0"
4202+
class="AnalyticalTable-tbody-0 sapScrollBar"
42034203
style="position: relative; height: 220px; width: 600px; overflow: auto; will-change: transform; direction: ltr;"
42044204
>
42054205
<div
@@ -4690,7 +4690,7 @@ exports[`AnalyticalTable test drag and drop of a draggable column 1`] = `
46904690
</div>
46914691
</header>
46924692
<div
4693-
class="AnalyticalTable-tbody-0"
4693+
class="AnalyticalTable-tbody-0 sapScrollBar"
46944694
style="position: relative; height: 220px; width: 600px; overflow: auto; will-change: transform; direction: ltr;"
46954695
>
46964696
<div
@@ -5231,7 +5231,7 @@ exports[`AnalyticalTable with highlight row 1`] = `
52315231
</div>
52325232
</header>
52335233
<div
5234-
class="AnalyticalTable-tbody-0 AnalyticalTable-selectable-0"
5234+
class="AnalyticalTable-tbody-0 sapScrollBar AnalyticalTable-selectable-0"
52355235
style="position: relative; height: 220px; width: 642px; overflow: auto; will-change: transform; direction: ltr;"
52365236
>
52375237
<div
@@ -5786,7 +5786,7 @@ exports[`AnalyticalTable without selection Column 1`] = `
57865786
</div>
57875787
</header>
57885788
<div
5789-
class="AnalyticalTable-tbody-0 AnalyticalTable-selectable-0"
5789+
class="AnalyticalTable-tbody-0 sapScrollBar AnalyticalTable-selectable-0"
57905790
style="position: relative; height: 220px; width: 600px; overflow: auto; will-change: transform; direction: ltr;"
57915791
>
57925792
<div

packages/main/src/components/AnalyticalTable/virtualization/VirtualTableBody.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { StyleClassHelper } from '@ui5/webcomponents-react-base/lib/StyleClassHe
44
import { TableSelectionMode } from '@ui5/webcomponents-react/lib/TableSelectionMode';
55
import React, { useCallback, useMemo, useRef } from 'react';
66
import { FixedSizeList } from 'react-window';
7+
import { GlobalStyleClasses } from '@ui5/webcomponents-react/lib/GlobalStyleClasses';
78
import { VirtualTableRow } from './VirtualTableRow';
89

910
export const VirtualTableBody = (props) => {
@@ -58,7 +59,7 @@ export const VirtualTableBody = (props) => {
5859
[prepareRow]
5960
);
6061

61-
const classNames = StyleClassHelper.of(classes.tbody);
62+
const classNames = StyleClassHelper.of(classes.tbody, GlobalStyleClasses.sapScrollBar);
6263
if (selectionMode === TableSelectionMode.SINGLE_SELECT || selectionMode === TableSelectionMode.MULTI_SELECT) {
6364
classNames.put(classes.selectable);
6465
}

packages/main/src/components/ObjectPage/ObjectPage.jss.ts

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,6 @@ const styles = {
1818
backgroundColor: ThemingParameters.sapBackgroundColor,
1919
overflowX: 'hidden',
2020
overflowY: 'auto',
21-
'&::-webkit-scrollbar': {
22-
backgroundColor: ThemingParameters.sapScrollBar_TrackColor,
23-
width: ThemingParameters.sapScrollBar_Dimension
24-
},
25-
'&::-webkit-scrollbar-thumb': {
26-
backgroundColor: ThemingParameters.sapScrollBar_BorderColor,
27-
'&:hover': {
28-
backgroundColor: ThemingParameters.sapScrollBar_Hover_FaceColor
29-
}
30-
},
31-
'&::-webkit-scrollbar-corner': {
32-
backgroundColor: ThemingParameters.sapScrollBar_TrackColor
33-
},
3421
'& section[id*="ObjectPageSection-"] > div[role="heading"]': {
3522
display: 'none'
3623
},

packages/main/src/components/ObjectPage/__snapshots__/ObjectPage.test.tsx.snap

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
exports[`ObjectPage IconTabBar Mode 1`] = `
44
<div
5-
class="ObjectPage-objectPage-0 ObjectPage-iconTabBarMode-0"
5+
class="ObjectPage-objectPage-0 sapScrollBar ObjectPage-iconTabBarMode-0"
66
data-component-name="ObjectPage"
77
>
88
<header
@@ -196,7 +196,7 @@ exports[`ObjectPage IconTabBar Mode 1`] = `
196196

197197
exports[`ObjectPage Just Some Sections 1`] = `
198198
<div
199-
class="ObjectPage-objectPage-0 ObjectPage-iconTabBarMode-0"
199+
class="ObjectPage-objectPage-0 sapScrollBar ObjectPage-iconTabBarMode-0"
200200
data-component-name="ObjectPage"
201201
>
202202
<header
@@ -312,7 +312,7 @@ exports[`ObjectPage Just Some Sections 1`] = `
312312

313313
exports[`ObjectPage Key Infos 1`] = `
314314
<div
315-
class="ObjectPage-objectPage-0"
315+
class="ObjectPage-objectPage-0 sapScrollBar"
316316
data-component-name="ObjectPage"
317317
>
318318
<header
@@ -521,7 +521,7 @@ exports[`ObjectPage Key Infos 1`] = `
521521

522522
exports[`ObjectPage No Header 1`] = `
523523
<div
524-
class="ObjectPage-objectPage-0 ObjectPage-noHeader-0"
524+
class="ObjectPage-objectPage-0 sapScrollBar ObjectPage-noHeader-0"
525525
data-component-name="ObjectPage"
526526
>
527527
<header
@@ -660,7 +660,7 @@ exports[`ObjectPage No Header 1`] = `
660660

661661
exports[`ObjectPage Not crashing with 0 sections 1`] = `
662662
<div
663-
class="ObjectPage-objectPage-0 ObjectPage-iconTabBarMode-0"
663+
class="ObjectPage-objectPage-0 sapScrollBar ObjectPage-iconTabBarMode-0"
664664
data-component-name="ObjectPage"
665665
>
666666
<header
@@ -735,7 +735,7 @@ exports[`ObjectPage Not crashing with 0 sections 1`] = `
735735

736736
exports[`ObjectPage Not crashing with 1 section - Default Mode 1`] = `
737737
<div
738-
class="ObjectPage-objectPage-0"
738+
class="ObjectPage-objectPage-0 sapScrollBar"
739739
data-component-name="ObjectPage"
740740
>
741741
<header
@@ -843,7 +843,7 @@ exports[`ObjectPage Not crashing with 1 section - Default Mode 1`] = `
843843

844844
exports[`ObjectPage Not crashing with 1 section - IconTabBar Mode 1`] = `
845845
<div
846-
class="ObjectPage-objectPage-0 ObjectPage-iconTabBarMode-0"
846+
class="ObjectPage-objectPage-0 sapScrollBar ObjectPage-iconTabBarMode-0"
847847
data-component-name="ObjectPage"
848848
>
849849
<header
@@ -951,7 +951,7 @@ exports[`ObjectPage Not crashing with 1 section - IconTabBar Mode 1`] = `
951951

952952
exports[`ObjectPage Only Sections 1`] = `
953953
<div
954-
class="ObjectPage-objectPage-0"
954+
class="ObjectPage-objectPage-0 sapScrollBar"
955955
data-component-name="ObjectPage"
956956
>
957957
<header
@@ -1181,7 +1181,7 @@ exports[`ObjectPage Only Sections 1`] = `
11811181

11821182
exports[`ObjectPage Set selected section id 1`] = `
11831183
<div
1184-
class="ObjectPage-objectPage-0 ObjectPage-iconTabBarMode-0"
1184+
class="ObjectPage-objectPage-0 sapScrollBar ObjectPage-iconTabBarMode-0"
11851185
data-component-name="ObjectPage"
11861186
>
11871187
<header
@@ -1297,7 +1297,7 @@ exports[`ObjectPage Set selected section id 1`] = `
12971297

12981298
exports[`ObjectPage With Subsections 1`] = `
12991299
<div
1300-
class="ObjectPage-objectPage-0"
1300+
class="ObjectPage-objectPage-0 sapScrollBar"
13011301
data-component-name="ObjectPage"
13021302
>
13031303
<header

packages/main/src/components/ObjectPage/index.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { enrichEventWithDetails, getScrollBarWidth } from '@ui5/webcomponents-re
66
import { FlexBox } from '@ui5/webcomponents-react/lib/FlexBox';
77
import { FlexBoxAlignItems } from '@ui5/webcomponents-react/lib/FlexBoxAlignItems';
88
import { FlexBoxDirection } from '@ui5/webcomponents-react/lib/FlexBoxDirection';
9+
import { GlobalStyleClasses } from '@ui5/webcomponents-react/lib/GlobalStyleClasses';
910
import { Label } from '@ui5/webcomponents-react/lib/Label';
1011
import { ObjectPageMode } from '@ui5/webcomponents-react/lib/ObjectPageMode';
1112
import { Title } from '@ui5/webcomponents-react/lib/Title';
@@ -348,7 +349,7 @@ const ObjectPage: FC<ObjectPagePropTypes> = forwardRef((props: ObjectPagePropTyp
348349
isMounted.current = true;
349350
}, [isMounted, setScrollbarWidth]);
350351

351-
const objectPageClasses = StyleClassHelper.of(classes.objectPage);
352+
const objectPageClasses = StyleClassHelper.of(classes.objectPage, GlobalStyleClasses.sapScrollBar);
352353
if (className) {
353354
objectPageClasses.put(className);
354355
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { ThemingParameters } from '@ui5/webcomponents-react-base/lib/ThemingParameters';
2+
import { GlobalStyleClasses } from '@ui5/webcomponents-react/lib/GlobalStyleClasses';
3+
4+
export const GlobalStyleClassesStyles = {
5+
'@global': {
6+
[`.${GlobalStyleClasses.sapScrollBar}`]: {
7+
'&::-webkit-scrollbar': {
8+
backgroundColor: ThemingParameters.sapScrollBar_TrackColor,
9+
width: ThemingParameters.sapScrollBar_Dimension
10+
},
11+
'&::-webkit-scrollbar-thumb': {
12+
backgroundColor: ThemingParameters.sapScrollBar_FaceColor,
13+
width: ThemingParameters.sapScrollBar_Dimension,
14+
borderRadius: '0.25rem',
15+
'&:hover': {
16+
backgroundColor: ThemingParameters.sapScrollBar_Hover_FaceColor
17+
}
18+
},
19+
'&::-webkit-scrollbar-corner': {
20+
backgroundColor: ThemingParameters.sapScrollBar_TrackColor
21+
}
22+
}
23+
}
24+
};

packages/main/src/components/ThemeProvider/index.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
import { root as sap_fiori_3 } from '@sap-theming/theming-base-content/content/Base/baseLib/sap_fiori_3/variables.json';
22
import { getTheme } from '@ui5/webcomponents-base/dist/config/Theme';
33
import { getRTL } from '@ui5/webcomponents-base/dist/config/RTL';
4+
import { createComponentStyles } from '@ui5/webcomponents-react-base/lib/createComponentStyles';
45
import { cssVariablesStyles } from '@ui5/webcomponents-react-base/lib/CssSizeVariables';
56
import { ThemingParameters } from '@ui5/webcomponents-react-base/lib/ThemingParameters';
67
import { ContentDensity } from '@ui5/webcomponents-react/lib/ContentDensity';
78
import { Themes } from '@ui5/webcomponents-react/lib/Themes';
89
import React, { FC, ReactNode, useEffect, useMemo } from 'react';
910
import { ThemeProvider as ReactJssThemeProvider } from 'react-jss';
1011
import { JSSTheme } from '../../interfaces/JSSTheme';
12+
import { GlobalStyleClassesStyles } from './GlobalStyleClasses.jss';
1113

1214
const themeMap = window['@ui5/webcomponents-react-theming'] || (window['@ui5/webcomponents-react-theming'] = new Map());
1315
themeMap.set('sap_fiori_3', sap_fiori_3);
16+
const useStyles = createComponentStyles(GlobalStyleClassesStyles);
1417

1518
const insertThemeDesignerParameters = (parameters = {}) => {
1619
let element = document.querySelector('head #ui5wcr-theming-parameters');
@@ -56,6 +59,7 @@ if (!document.querySelector('style[data-ui5-webcomponents-react-sizes]')) {
5659
const ThemeProvider: FC<ThemeProviderProps> = (props: ThemeProviderProps) => {
5760
const { children, theme = getTheme() } = props;
5861
const isCompactSize = document.body.classList.contains('ui5-content-density-compact');
62+
useStyles();
5963

6064
useEffect(() => {
6165
if (themeMap) {
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export enum GlobalStyleClasses {
2+
sapScrollBar = 'sapScrollBar'
3+
}

packages/main/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ import { FlexBoxWrap } from './lib/FlexBoxWrap';
4747
import { Form } from './lib/Form';
4848
import { FormGroup } from './lib/FormGroup';
4949
import { FormItem } from './lib/FormItem';
50+
import { GlobalStyleClasses } from './lib/GlobalStyleClasses';
5051
import { Grid } from './lib/Grid';
5152
import { GridPosition } from './lib/GridPosition';
5253
import { GroupHeaderListItem } from './lib/GroupHeaderListItem';
@@ -181,6 +182,7 @@ export {
181182
Form,
182183
FormGroup,
183184
FormItem,
185+
GlobalStyleClasses,
184186
Grid,
185187
GridPosition,
186188
GroupHeaderListItem,
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { GlobalStyleClasses } from '../enums/GlobalStyleClasses';
2+
3+
export { GlobalStyleClasses };

0 commit comments

Comments
 (0)