Skip to content

Commit

Permalink
refactor(overlay): support an async and a sync loading of dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
Westbrook committed Aug 11, 2020
1 parent fc122b3 commit 4d0b2ef
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 12 deletions.
6 changes: 6 additions & 0 deletions packages/overlay/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ import '@spectrum-web-components/overlay/active-overlay.js';
import '@spectrum-web-components/overlay/overlay-trigger.js';
```

The default of `<overlay-trigger>` will load various dependencies asynchronously via a dynamic import. In the case that you would like to import those tranverse dependencies statically, import the side effectful registration of `<overlay-trigger>` as follows:

```
import '@spectrum-web-components/overlay/sync/overlay-trigger.js';
```

When looking to leverage the `ActiveOverlay` or `OverlayTrigger` base class as a type and/or for extension purposes, do so via:

```
Expand Down
6 changes: 5 additions & 1 deletion packages/overlay/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@
"./active-overlay": "./active-overlay.js",
"./active-overlay.js": "./active-overlay.js",
"./overlay-trigger": "./overlay-trigger.js",
"./overlay-trigger.js": "./overlay-trigger.js"
"./overlay-trigger.js": "./overlay-trigger.js",
"./sync/overlay-trigger": "./sync/overlay-trigger.js",
"./sync/overlay-trigger.js": "./sync/overlay-trigger.js"
},
"files": [
"custom-elements.json",
Expand All @@ -44,6 +46,8 @@
"./active-overlay.ts",
"./overlay-trigger.js",
"./overlay-trigger.ts",
"./sync/overlay-trigger.js",
"./sync/overlay-trigger.ts",
"./stories/overlay-story-components.js",
"./stories/overlay-story-components.ts"
],
Expand Down
31 changes: 21 additions & 10 deletions packages/overlay/src/OverlayTrigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ import {
TemplateResult,
} from 'lit-element';

import { Placement, TriggerInteractions } from './overlay-types';
import {
Placement,
TriggerInteractions,
OverlayOptions,
} from './overlay-types';
import { openOverlay } from './loader.js';
import overlayTriggerStyles from './overlay-trigger.css.js';

Expand Down Expand Up @@ -83,17 +87,22 @@ export class OverlayTrigger extends LitElement {
`;
}

private async openOverlay(
public static openOverlay = async (
target: HTMLElement,
interaction: TriggerInteractions,
content: HTMLElement,
interaction: TriggerInteractions
): Promise<() => void> {
return await openOverlay(target, interaction, content, {
options: OverlayOptions
): Promise<() => void> => {
return await openOverlay(target, interaction, content, options);
};

private get overlayOptions(): OverlayOptions {
return {
offset: this.offset,
placement: this.placement,
receivesFocus:
this.type && this.type !== 'inline' ? 'auto' : undefined,
});
};
}

private onTrigger(event: Event): void {
Expand Down Expand Up @@ -125,10 +134,11 @@ export class OverlayTrigger extends LitElement {
}
}
const { targetContent, clickContent } = this;
this.closeClickOverlay = await this.openOverlay(
this.closeClickOverlay = await OverlayTrigger.openOverlay(
targetContent,
this.type ? this.type : 'click',
clickContent,
this.type ? this.type : 'click'
this.overlayOptions
);
}
}
Expand All @@ -145,10 +155,11 @@ export class OverlayTrigger extends LitElement {
overlayReady = res;
});
const { targetContent, hoverContent } = this;
this.closeHoverOverlay = await this.openOverlay(
this.closeHoverOverlay = await OverlayTrigger.openOverlay(
targetContent,
'hover',
hoverContent,
'hover'
this.overlayOptions
);
overlayReady();
}
Expand Down
28 changes: 28 additions & 0 deletions packages/overlay/sync/overlay-trigger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
Copyright 2020 Adobe. All rights reserved.
This file is licensed to you under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
OF ANY KIND, either express or implied. See the License for the specific language
governing permissions and limitations under the License.
*/

import { OverlayTrigger } from '../src/OverlayTrigger.js';
import {
Overlay,
OverlayOptions,
TriggerInteractions,
} from '@spectrum-web-components/overlay';
import '../overlay-trigger.js';

OverlayTrigger.openOverlay = async (
target: HTMLElement,
interaction: TriggerInteractions,
content: HTMLElement,
options: OverlayOptions
): Promise<() => void> => {
return await Overlay.open(target, interaction, content, options);
};
2 changes: 1 addition & 1 deletion packages/overlay/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"composite": true,
"rootDir": "./"
},
"include": ["*.ts", "src/*.ts"],
"include": ["*.ts", "src/*.ts", "sync/*.ts"],
"exclude": ["test/*.ts", "stories/*.ts"],
"references": [{ "path": "../theme" }]
}

0 comments on commit 4d0b2ef

Please sign in to comment.