-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from jjang16/main
registers SafeAreaElement in browser only in order to prevent breaking evaluating SafeAreaController in SSR environments.
- Loading branch information
Showing
6 changed files
with
94 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { SafeArea } from './index'; | ||
|
||
export class SafeAreaElement extends HTMLElement { | ||
mode: 'padding' | 'margin'; | ||
edges?: string; | ||
|
||
constructor() { | ||
super(); | ||
|
||
this.mode = 'padding'; | ||
// Create a shadow root | ||
this.attachShadow({ mode: 'open' }); | ||
} | ||
|
||
connectedCallback(): void { | ||
this.renderElement(); | ||
} | ||
|
||
static get observedAttributes(): string[] { | ||
return ['mode', 'edges']; | ||
} | ||
|
||
attributeChangedCallback( | ||
name: string, | ||
oldValue: string, | ||
newValue: string, | ||
): void { | ||
if (oldValue === newValue) { | ||
return; | ||
} | ||
(this as any)[name] = newValue; | ||
this.renderElement(); | ||
} | ||
|
||
async renderElement(): Promise<void> { | ||
if (this.shadowRoot) { | ||
const safeAreaInset = await SafeArea.getSafeAreaInsets(); | ||
|
||
const isMargin = this.mode === 'margin'; | ||
const edges = this.edges?.split(',').map(item => item.trim()); | ||
|
||
const insets = { | ||
top: edges && !edges?.includes('top') ? 0 : safeAreaInset.top, | ||
bottom: edges && !edges?.includes('bottom') ? 0 : safeAreaInset.bottom, | ||
left: edges && !edges?.includes('left') ? 0 : safeAreaInset.left, | ||
right: edges && !edges?.includes('right') ? 0 : safeAreaInset.right, | ||
}; | ||
|
||
this.shadowRoot.innerHTML = ` | ||
<style> | ||
#wrapper { | ||
${isMargin ? 'margin' : 'padding'}-top: ${insets.top}px; | ||
${isMargin ? 'margin' : 'padding'}-bottom: ${insets.bottom}px; | ||
${isMargin ? 'margin' : 'padding'}-left: ${insets.left}px; | ||
${isMargin ? 'margin' : 'padding'}-right: ${insets.right}px; | ||
} | ||
</style> | ||
<div id="wrapper"> | ||
<slot></slot> | ||
</div> | ||
`; | ||
} | ||
} | ||
} | ||
|
||
export const registerSafeAreaElement = (): void => { | ||
if (!customElements.get('safe-area')) | ||
customElements.define('safe-area', SafeAreaElement); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,13 @@ | ||
import { WebPlugin } from '@capacitor/core'; | ||
|
||
import type { SafeAreaPlugin, SafeAreaType } from './definitions'; | ||
import type { SafeAreaPlugin, SafeAreaInset } from './definitions'; | ||
|
||
export class SafeAreaWeb extends WebPlugin implements SafeAreaPlugin { | ||
async getStatusBarHeight(): Promise<{ height: number }> { | ||
return { height: 0 }; | ||
} | ||
|
||
async getSafeAreaInsets(): Promise<SafeAreaType> { | ||
async getSafeAreaInsets(): Promise<SafeAreaInset> { | ||
return { top: 0, bottom: 0, left: 0, right: 0 }; | ||
} | ||
} |