Skip to content

Commit 25f514a

Browse files
benotternmetulev
authored andcommitted
[feature] Added property to toggle ShadowDom in all, or per component (#85)
* Added ability to disable ShadowDOM on a per component basis * Updated order of property check to base first, then self * Added fallback styles when disabling shadow dom * Last Commit for Ben! * prettier write
1 parent dd6900a commit 25f514a

File tree

13 files changed

+323
-156
lines changed

13 files changed

+323
-156
lines changed

docs/style.md

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
2-
title: "Styling the Microsoft Graph Toolkit"
3-
description: "Use CSS custom properties to modify the component styles"
2+
title: 'Styling the Microsoft Graph Toolkit'
3+
description: 'Use CSS custom properties to modify the component styles'
44
localization_priority: Normal
55
author: nmetulev
66
---
@@ -19,4 +19,22 @@ mgt-person {
1919

2020
It is not possible to style internal elements of a component unless a CSS custom property is provided. The component children elements are hosted in a [shadow dom](https://developer.mozilla.org/en-US/docs/Web/Web_Components/Using_shadow_DOM).
2121

22-
For more flexibility, consider using [custom templates](./templates.md).
22+
For more flexibility, consider using [custom templates](./templates.md).
23+
24+
## Addendum:
25+
26+
( Experimental! )
27+
28+
It is now possible to disable the Shadow Dom, and directly style internal elements using normal browser stylesheets, by setting the static property 'useShadowDom' of the MgtBaseComponent class to false before using any MGT Tags:
29+
30+
Example:
31+
32+
```html
33+
<script type="module">
34+
import { MgtBaseComponent } from './dist/es6/components/baseComponent.js';
35+
36+
MgtBaseComponent.useShadowDom = false;
37+
</script>
38+
39+
<script type="module" src="./dist/es6/components/mgt-tasks/mgt-tasks.js"></script>
40+
````

src/bundle/index.es6.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*/
77

88
export * from '../index';
9+
export * from '../components/baseComponent';
910
export * from '../components/providers/providers';
1011
export * from '../providers/providers';
1112
export * from '../mock/mgt-mock-provider';

src/components/baseComponent.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,25 @@ export abstract class MgtBaseComponent extends LitElement {
1616
});
1717
return this.dispatchEvent(event);
1818
}
19+
20+
private static _useShadowRoot: boolean = true;
21+
public static get useShadowRoot() {
22+
return this._useShadowRoot;
23+
}
24+
public static set useShadowRoot(value: boolean) {
25+
this._useShadowRoot = value;
26+
}
27+
28+
constructor() {
29+
super();
30+
if (this.isShadowRootDisabled()) this['_needsShimAdoptedStyleSheets'] = true;
31+
}
32+
33+
protected createRenderRoot() {
34+
return this.isShadowRootDisabled() ? this : super.createRenderRoot();
35+
}
36+
37+
public isShadowRootDisabled() {
38+
return !MgtBaseComponent._useShadowRoot || !(this.constructor as typeof MgtBaseComponent)._useShadowRoot;
39+
}
1940
}

src/components/mgt-agenda/mgt-agenda.scss

Lines changed: 44 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,21 @@
88
@import '../../../node_modules/office-ui-fabric-core/dist/sass/References';
99
@import '../../styles/shared-styles.scss';
1010

11-
.header {
11+
:host .header,
12+
mgt-agenda .header {
1213
margin: var(--agenda-header-margin, 40px 10px 14px 10px);
1314
font-family: var(--default-font-family);
1415
font-size: var(--agenda-header-font-size, 24px);
1516
color: var(--agenda-header-color, #333333);
1617
}
1718

18-
.agenda > .group:first-child > .header {
19+
:host .agenda > .group:first-child > .header,
20+
mgt-agenda .agenda > .group:first-child > .header {
1921
margin-top: 0;
2022
}
2123

22-
.agenda-list {
24+
:host .agenda-list,
25+
mgt-agenda .agenda-list {
2326
list-style-type: none;
2427
padding: 0;
2528
margin: 0;
@@ -28,7 +31,8 @@
2831
font-weight: normal;
2932
}
3033

31-
.event {
34+
:host .event,
35+
mgt-agenda .event {
3236
background: var(--event-background, #ffffff);
3337
border: var(--event-border, solid 2px rgba(0, 0, 0, 0));
3438
box-shadow: var(--event-box-shadow, 0px 2px 8px rgba(0, 0, 0, 0.092));
@@ -37,95 +41,113 @@
3741
display: flex;
3842
}
3943

40-
.narrow .event {
44+
:host .narrow .event,
45+
mgt-agenda .narrow .event {
4146
flex-direction: column;
4247
}
4348

44-
.event-time-container {
49+
:host .event-time-container,
50+
mgt-agenda .event-time-container {
4551
display: flex;
4652
flex-direction: column;
4753
margin: 18px 38px 18px 18px;
4854
width: 120px;
4955
}
5056

51-
.narrow .event-time-container {
57+
:host .narrow .event-time-container,
58+
mgt-agenda .narrow .event-time-container {
5259
margin: 14px 18px 8px 18px;
5360
}
5461

55-
.event-time {
62+
:host .event-time,
63+
mgt-agenda .event-time {
5664
font-size: var(--event-time-font-size, 12px);
5765
color: var(--event-time-color, #000000);
5866
font-weight: 600;
5967
}
6068

61-
.event-duration {
69+
:host .event-duration,
70+
mgt-agenda .event-duration {
6271
color: rgba(16, 16, 16, 0.3);
6372
}
6473

65-
.event-details-container {
74+
:host .event-details-container,
75+
mgt-agenda .event-details-container {
6676
margin: 14px 18px 14px 0px;
6777
}
6878

69-
.narrow .event-details-container {
79+
:host .narrow .event-details-container,
80+
mgt-agenda .narrow .event-details-container {
7081
margin: 0px 18px 14px 18px;
7182
}
7283

73-
.event-subject {
84+
:host .event-subject,
85+
mgt-agenda .event-subject {
7486
margin: 0px 18px 0px 0px;
7587
font-size: var(--event-subject-font-size, 19px);
7688
color: var(--event-subject-color, #333333);
7789
}
7890

79-
.event-location-container {
91+
:host .event-location-container,
92+
mgt-agenda .event-location-container {
8093
display: flex;
8194
margin: 8px 18px 0px 0px;
8295
}
8396

84-
.event-location {
97+
:host .event-location,
98+
mgt-agenda .event-location {
8599
font-size: var(--event-location-font-size, 12px);
86100
color: var(--event-location-color, #000000);
87101
margin: 0px 0px 0px 4px;
88102
}
89103

90-
.event-attendees {
104+
:host .event-attendees,
105+
mgt-agenda .event-attendees {
91106
--list-margin: 8px 0 0 0;
92107
--avatar-size-s: 20px;
93108
}
94109

95-
.event-other-container {
110+
:host .event-other-container,
111+
mgt-agenda .event-other-container {
96112
margin: 2px 16px 4px 16px;
97113
margin-left: auto;
98114
}
99115

100116
// loading style
101117

102-
.loading-element {
118+
:host .loading-element,
119+
mgt-agenda .loading-element {
103120
background: #f2f2f2;
104121
border-radius: 1px;
105122
}
106123

107-
.event-time-loading {
124+
:host .event-time-loading,
125+
mgt-agenda .event-time-loading {
108126
width: 100px;
109127
height: 10px;
110128
}
111129

112-
.event-subject-loading {
130+
:host .event-subject-loading,
131+
mgt-agenda .event-subject-loading {
113132
width: 263px;
114133
height: 16px;
115134
}
116135

117-
.event-location-icon-loading {
136+
:host .event-location-icon-loading,
137+
mgt-agenda .event-location-icon-loading {
118138
width: 14px;
119139
height: 14px;
120140
}
121141

122-
.event-location-loading {
142+
:host .event-location-loading,
143+
mgt-agenda .event-location-loading {
123144
width: 90px;
124145
height: 10px;
125146
margin: 2px 0px 0px 4px;
126147
}
127148

128-
.event-attendee-loading {
149+
:host .event-attendee-loading,
150+
mgt-agenda .event-attendee-loading {
129151
width: 20px;
130152
height: 20px;
131153
border-radius: 10px;

src/components/mgt-login/mgt-login.scss

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
@import '../../../node_modules/office-ui-fabric-core/dist/sass/References';
99
@import '../../styles/shared-styles.scss';
1010

11-
:host {
11+
:host,
12+
mgt-login {
1213
--font-size: #{$ms-font-size-m};
1314
--font-weight: #{$ms-font-weight-semibold};
1415
--height: '100%';
@@ -21,12 +22,14 @@
2122
--popup-command-font-size: #{$ms-font-size-s};
2223
}
2324

24-
.root {
25+
:host .root,
26+
mgt-login .root {
2527
position: relative;
2628
display: inline-block;
2729
}
2830

29-
.login-button {
31+
:host .login-button,
32+
mgt-login .login-button {
3033
display: flex;
3134
align-items: center;
3235
font-family: var(--default-font-family);
@@ -56,13 +59,15 @@
5659
}
5760
}
5861

59-
.login-icon {
62+
:host .login-icon,
63+
mgt-login .login-icon {
6064
& + span {
6165
margin-left: 6px;
6266
}
6367
}
6468

65-
.popup {
69+
:host .popup,
70+
mgt-login .popup {
6671
display: none;
6772
position: absolute;
6873
animation-duration: 300ms;
@@ -82,19 +87,22 @@
8287
}
8388
}
8489

85-
.popup-content {
90+
:host .popup-content,
91+
mgt-login .popup-content {
8692
display: flex;
8793
flex-direction: column;
8894
padding: 24px 48px 16px 24px;
8995
}
9096

91-
.popup-commands ul {
97+
:host .popup-commands ul,
98+
mgt-login .popup-commands ul {
9299
list-style-type: none;
93100
margin: 16px 0 0;
94101
padding: 0;
95102
}
96103

97-
.popup-command {
104+
:host .popup-command,
105+
mgt-login .popup-command {
98106
font-family: var(--default-font-family);
99107
font-size: var(--popup-command-font-size);
100108
font-weight: var(--font-weight);

src/components/mgt-login/mgt-login.ts

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,20 @@
55
* -------------------------------------------------------------------------------------------
66
*/
77

8-
import { LitElement, html, customElement, property } from 'lit-element';
8+
import { html, customElement, property } from 'lit-element';
99
import * as MicrosoftGraph from '@microsoft/microsoft-graph-types';
1010

1111
import { Providers } from '../../Providers';
1212
import { ProviderState } from '../../providers/IProvider';
13+
import { MgtBaseComponent } from '../baseComponent';
1314
import { styles } from './mgt-login-css';
1415

1516
import { MgtPersonDetails } from '../mgt-person/mgt-person';
1617
import '../mgt-person/mgt-person';
1718
import '../../styles/fabric-icon-font';
1819

1920
@customElement('mgt-login')
20-
export class MgtLogin extends LitElement {
21+
export class MgtLogin extends MgtBaseComponent {
2122
private _loginButtonRect: ClientRect;
2223
private _popupRect: ClientRect;
2324
private _openLeft: boolean = false;
@@ -42,18 +43,10 @@ export class MgtLogin extends LitElement {
4243
this.loadState();
4344
}
4445

45-
private fireCustomEvent(eventName: string): boolean {
46-
let event = new CustomEvent(eventName, {
47-
cancelable: true,
48-
bubbles: false
49-
});
50-
return this.dispatchEvent(event);
51-
}
52-
5346
updated(changedProps) {
5447
if (changedProps.get('_showMenu') === false) {
5548
// get popup bounds
56-
const popup = this.shadowRoot.querySelector('.popup');
49+
const popup = this.renderRoot.querySelector('.popup');
5750
if (popup && popup.animate) {
5851
this._popupRect = popup.getBoundingClientRect();
5952

@@ -126,16 +119,16 @@ export class MgtLogin extends LitElement {
126119
}
127120

128121
firstUpdated() {
129-
window.onclick = (event: any) => {
122+
window.addEventListener('click', (event: MouseEvent) => {
130123
if (event.target !== this) {
131124
// get popup bounds
132-
const popup = this.shadowRoot.querySelector('.popup');
125+
const popup = this.renderRoot.querySelector('.popup');
133126
if (popup) {
134127
this._popupRect = popup.getBoundingClientRect();
135128
this._showMenu = false;
136129
}
137130
}
138-
};
131+
});
139132
}
140133

141134
private async loadState() {
@@ -160,10 +153,11 @@ export class MgtLogin extends LitElement {
160153
this._loading = false;
161154
}
162155

163-
private onClick() {
156+
private onClick(e: MouseEvent) {
157+
e.stopPropagation();
164158
if (this._user || this.userDetails) {
165159
// get login button bounds
166-
const loginButton = this.shadowRoot.querySelector('.login-button');
160+
const loginButton = this.renderRoot.querySelector('.login-button');
167161
if (loginButton) {
168162
this._loginButtonRect = loginButton.getBoundingClientRect();
169163

0 commit comments

Comments
 (0)