Skip to content

Commit 7ca32db

Browse files
authored
fix(playground): tabs stackblitz example loads in Safari (#3287)
1 parent a006c22 commit 7ca32db

File tree

3 files changed

+26
-6
lines changed

3 files changed

+26
-6
lines changed

src/components/global/Playground/index.tsx

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -546,10 +546,30 @@ export default function Playground({
546546

547547
if (hasUsageTargetOptions) {
548548
editorOptions.files = Object.keys(codeSnippets[usageTarget])
549-
.map((fileName) => ({
550-
[fileName]: hostRef.current!.querySelector<HTMLElement>(`#${getCodeSnippetId(usageTarget, fileName)} code`)
551-
.outerText,
552-
}))
549+
.map((fileName) => {
550+
const codeBlock = hostRef.current!.querySelector<HTMLElement>(
551+
`#${getCodeSnippetId(usageTarget, fileName)} code`
552+
);
553+
let code = codeBlock.outerText;
554+
555+
if (code.trim().length === 0) {
556+
/**
557+
* Safari has an issue where accessing the `outerText` on a non-visible
558+
* DOM element results in a string with only whitespace. To work around this,
559+
* we create a clone of the element, not attached to the DOM, and parse
560+
* the outerText from that.
561+
*
562+
* Only in Safari does this persist whitespace & line breaks, so we
563+
* explicitly check for when the code is empty to use this workaround.
564+
*/
565+
const el = document.createElement('div');
566+
el.innerHTML = codeBlock.innerHTML;
567+
code = el.outerText;
568+
}
569+
return {
570+
[fileName]: code,
571+
};
572+
})
553573
.reduce((acc, curr) => ({ ...acc, ...curr }), {});
554574
editorOptions.dependencies = (code[usageTarget] as UsageTargetOptions).dependencies;
555575
}

static/usage/v6/tabs/router/angular/app_module_ts.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { ExampleComponent } from './example.component';
1111
import { AppRoutingModule } from './app-routing.module';
1212

1313
@NgModule({
14-
imports: [BrowserModule, FormsModule, AppRoutingModule, IonicModule.forRoot()],
14+
imports: [BrowserModule, FormsModule, AppRoutingModule, IonicModule.forRoot({})],
1515
declarations: [AppComponent, ExampleComponent],
1616
bootstrap: [AppComponent],
1717
})

static/usage/v7/tabs/router/angular/app_module_ts.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { ExampleComponent } from './example.component';
1111
import { AppRoutingModule } from './app-routing.module';
1212

1313
@NgModule({
14-
imports: [BrowserModule, FormsModule, AppRoutingModule, IonicModule.forRoot()],
14+
imports: [BrowserModule, FormsModule, AppRoutingModule, IonicModule.forRoot({})],
1515
declarations: [AppComponent, ExampleComponent],
1616
bootstrap: [AppComponent],
1717
})

0 commit comments

Comments
 (0)