Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add fallback component #663

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions lib/CustomFallbackComponent.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<script>
export let blok;
</script>

<section>
<div>
<p class="bg-purple-500">
Component could not be found for blok <span class="component"
>{blok.component}</span
>! Is it configured correctly?
</p>
</div>
</section>
13 changes: 13 additions & 0 deletions lib/FallbackComponent.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<script>
export let blok;
</script>

<section>
<div>
<p class="bg-red-500">
Component could not be found for blok <span class="component"
>{blok.component}</span
>! Is it configured correctly?
</p>
</div>
</section>
21 changes: 18 additions & 3 deletions lib/pkg/StoryblokComponent.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
<script>
import { getComponent } from "./index";
import FallbackComponent from "../FallbackComponent.svelte";
import CustomFallbackComponent from "../CustomFallbackComponent.svelte";
import {
getComponent,
getFallbackComponent,
getCustomFallbackComponent,
} from "./index";

let component;
export let blok;
Expand All @@ -11,5 +17,14 @@
}
</script>

<!-- svelte-ignore missing-declaration -->
<svelte:component this={component} {blok} {...$$restProps} />
{#if component}
<svelte:component this={component} {blok} {...$$restProps} />
{:else if getFallbackComponent() && getCustomFallbackComponent()}
<CustomFallbackComponent {blok} />
{:else if getFallbackComponent()}
<FallbackComponent {blok} />
{:else}
{console.error(
`Component could not be found for blok "${blok.component}"! Is it defined in +layout.js?`
)}
{/if}
24 changes: 13 additions & 11 deletions lib/pkg/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import type {
StoryblokClient,
SbBlokData,
} from "./types";
import type { SvelteComponent } from "svelte";

export const storyblokEditable = (node: HTMLElement, value: SbBlokData) => {
const updateDom = (value: SbBlokData) => {
Expand Down Expand Up @@ -49,11 +50,15 @@ export const useStoryblokApi = (): StoryblokClient => {
export { useStoryblokApi as getStoryblokApi };

let componentsMap: SbSvelteComponentsMap | CallableFunction = null;
let fallbackComponent: boolean = false;
let customFallbackComponent: SvelteComponent<any> = null;

export const storyblokInit = (options: SbSvelteSDKOptions) => {
const { storyblokApi } = sbInit(options);
storyblokApiInstance = storyblokApi;
componentsMap = options.components || {};
fallbackComponent = options.fallbackComponent;
customFallbackComponent = options.customFallbackComponent;
};

export const getComponent = (componentName: string) => {
Expand All @@ -63,19 +68,16 @@ export const getComponent = (componentName: string) => {
? componentsMap()[componentName]
: componentsMap[componentName];

if (!component) {
console.error(`You didn't load the ${componentName} component. Please load it in storyblokInit. For example:
storyblokInit({
accessToken: "<your-token>",
components: {
"teaser": Teaser
}
})
`);
}

return component;
};

export const getFallbackComponent = () => {
return fallbackComponent;
};

export const getCustomFallbackComponent = () => {
return customFallbackComponent;
};

export * from "./types";
export { default as StoryblokComponent } from "./StoryblokComponent.svelte";
2 changes: 2 additions & 0 deletions lib/pkg/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ export interface SbSvelteComponentsMap {

export interface SbSvelteSDKOptions extends SbSDKOptions {
components?: SbSvelteComponentsMap | CallableFunction;
fallbackComponent?: boolean;
customFallbackComponent?: SvelteComponent<any>;
}

export { apiPlugin } from "@storyblok/js";
Expand Down
Loading