@sevenfold/setto-client 0.48.0 → 0.49.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -33,6 +33,17 @@ export interface SettoLanguagesProps {
33
33
  */
34
34
  inline?: boolean;
35
35
  }
36
+ /**
37
+ * Whether an inline switcher has been drawn in this render, ahead of the
38
+ * floating copy (the provider draws that after the site's tree). Set while
39
+ * drawing rather than from an effect, so a page drawn on a server — and the
40
+ * browser's first render of it — leaves the floating copy out from the start
41
+ * (0.49.0). It was in the prerendered HTML of setto.no and vanished once the
42
+ * page's script ran: a «Norsk» pill blinking in the corner of every page.
43
+ */
44
+ export declare const InlineLanguagesContext: import("react").Context<{
45
+ drawn: boolean;
46
+ } | null>;
36
47
  export declare function SettoLanguages({ languages: given, inline }: SettoLanguagesProps): import("react/jsx-runtime").JSX.Element | null;
37
48
  /**
38
49
  * The floating copy, mounted by the provider.
@@ -23,6 +23,7 @@ import { type ComponentType } from 'react';
23
23
  import type { RootContent } from 'mdast';
24
24
  import { type BuiltinSectionProps } from '../pages/SettoPage.js';
25
25
  import { type BlogFrontmatter } from './contract.js';
26
+ import { type BlogPostData } from './data.js';
26
27
  export declare function SettoBlogSection({ sectionId, first }: BuiltinSectionProps): import("react/jsx-runtime").JSX.Element;
27
28
  /**
28
29
  * The blog for a site that routes by hand instead of through `pages.json`
@@ -30,10 +31,26 @@ export declare function SettoBlogSection({ sectionId, first }: BuiltinSectionPro
30
31
  * path to `settoBlog({ blogPath })`.
31
32
  *
32
33
  * <Route path="/blogg/*" element={<SettoBlog basePath="/blogg" />} />
34
+ *
35
+ * Both of the rest are optional, and are what a page drawn ahead of time
36
+ * needs to come out the same on the server and in the browser's first render
37
+ * (0.49.0), so `hydrateRoot` can take it over:
38
+ *
39
+ * <SettoBlog basePath="/blogg" path={useLocation().pathname} initialPost={post} />
40
+ *
41
+ * - `path`: the address within the router (`useLocation().pathname`, the
42
+ * language prefix already taken off). Without it the blog reads
43
+ * `window.location`, which a server does not have.
44
+ * - `initialPost`: the post being shown, already loaded (`blog.load(slug,
45
+ * locale)`). Used when it is the one this address shows, in place of
46
+ * fetching it in an effect; without it the post's first render is «Henter
47
+ * innlegget …», as before.
33
48
  */
34
- export declare function SettoBlog({ basePath, title }: {
49
+ export declare function SettoBlog({ basePath, title, path, initialPost, }: {
35
50
  basePath?: string;
36
51
  title?: string;
52
+ path?: string;
53
+ initialPost?: BlogPostData | null;
37
54
  }): import("react/jsx-runtime").JSX.Element;
38
55
  /** A post not yet published, as setto-server sends it: the frontmatter and the body's tree, each node with `at`. */
39
56
  export interface BlogDraftData {
@@ -1933,6 +1933,7 @@ function SettoProvider({ config, children }) {
1933
1933
  }, [i18n, languages]);
1934
1934
  useEffect(() => {
1935
1935
  if (languages.length < 2) return;
1936
+ if (document.head.querySelector('link[rel="alternate"][hreflang]:not([data-setto-alternate])')) return;
1936
1937
  const links = alternateLinks(window.location.origin, window.location.pathname, languages);
1937
1938
  const added = links.map(({ hreflang, href }) => {
1938
1939
  const el = document.createElement("link");
@@ -1999,9 +2000,10 @@ function SettoProvider({ config, children }) {
1999
2000
  guestEdit,
2000
2001
  setGuestEdit
2001
2002
  };
2003
+ const inlineLanguages = useRef({ drawn: false }).current;
2002
2004
  const editShellActive = editMode && !!value.store;
2003
2005
  const menuShown = canEdit && !!value.store;
2004
- return /* @__PURE__ */ jsx(SettoContext.Provider, { value, children: /* @__PURE__ */ jsxs(Fragment, { children: [
2006
+ return /* @__PURE__ */ jsx(SettoContext.Provider, { value, children: /* @__PURE__ */ jsxs(InlineLanguagesContext.Provider, { value: inlineLanguages, children: [
2005
2007
  /* @__PURE__ */ jsx(EditModeShell, { active: editShellActive, menu: menuShown, themeStore: value.themeStore, children }),
2006
2008
  /* @__PURE__ */ jsx(SettoChrome, {}),
2007
2009
  /* @__PURE__ */ jsx(SettoChat, {}),
@@ -2035,6 +2037,7 @@ function useVisitorView() {
2035
2037
  return visitorView({ authLoading, hasSession: !!session, sitesLoaded: sites !== null, canEdit, guestEdit });
2036
2038
  }
2037
2039
  const INLINE_MARKER = "data-setto-languages-inline";
2040
+ const InlineLanguagesContext = createContext(null);
2038
2041
  function SettoLanguages({ languages: given, inline = false }) {
2039
2042
  const { i18n } = useTranslation();
2040
2043
  const setto = useContext(SettoContext);
@@ -2044,6 +2047,8 @@ function SettoLanguages({ languages: given, inline = false }) {
2044
2047
  const [hover, setHover] = useState(false);
2045
2048
  const [surface, setSurface] = useState(null);
2046
2049
  const ref = useRef(null);
2050
+ const drawnInline = useContext(InlineLanguagesContext);
2051
+ if (inline && drawnInline) drawnInline.drawn = true;
2047
2052
  useEffect(() => {
2048
2053
  if (!inline) return;
2049
2054
  const root = document.documentElement;
@@ -2128,7 +2133,8 @@ function SettoLanguages({ languages: given, inline = false }) {
2128
2133
  ] });
2129
2134
  }
2130
2135
  function AutoLanguageSwitcher({ languages }) {
2131
- const [inlineElsewhere, setInlineElsewhere] = useState(false);
2136
+ const drawnInline = useContext(InlineLanguagesContext);
2137
+ const [inlineElsewhere, setInlineElsewhere] = useState(() => drawnInline?.drawn ?? false);
2132
2138
  useEffect(() => {
2133
2139
  const check = () => setInlineElsewhere(document.documentElement.hasAttribute(INLINE_MARKER));
2134
2140
  check();
@@ -3775,15 +3781,16 @@ function YouTubeEmbed({ id, title, language }) {
3775
3781
  ] });
3776
3782
  }
3777
3783
  const LATEST_COUNT = 3;
3778
- function useBlogContext() {
3784
+ function useBlogContext(path) {
3779
3785
  const { config } = useSetto();
3780
3786
  const { i18n, t } = useTranslation();
3781
3787
  const manifest = usePageManifest();
3782
3788
  const language = i18n.language || config.languages?.[0] || "no";
3783
3789
  const fallbackLanguage = config.languages?.[0] ?? "no";
3784
- const route = typeof window === "undefined" ? { basename: "", rest: "/" } : languageFromPath(window.location.pathname, config.languages ?? [fallbackLanguage]);
3785
- const href = (path) => `${route.basename}${path}`;
3786
- return { config, t, manifest, language, fallbackLanguage, strings: blogStrings(language), path: route.rest, href };
3790
+ const languages = config.languages ?? [fallbackLanguage];
3791
+ const route = typeof window === "undefined" ? { basename: language === fallbackLanguage || !languages.includes(language) ? "" : `/${language}`, rest: "/" } : languageFromPath(window.location.pathname, languages);
3792
+ const href = (to) => `${route.basename}${to}`;
3793
+ return { config, t, manifest, language, fallbackLanguage, strings: blogStrings(language), path: path ?? route.rest, href };
3787
3794
  }
3788
3795
  function blogPathOf(manifest) {
3789
3796
  return manifest && blogPage(manifest)?.page.path || BLOG_ROUTE;
@@ -3799,16 +3806,21 @@ function SettoBlogSection({ sectionId, first }) {
3799
3806
  const clear = useClearFixedHeader(inner, first);
3800
3807
  return /* @__PURE__ */ jsx(SettoSection, { sectionId, className: "setto-blog", children: /* @__PURE__ */ jsx("div", { className: "setto-blog__inner", ref: inner, style: clear ? { paddingTop: clear } : void 0, children: slug ? /* @__PURE__ */ jsx(BlogPostView, { slug, blogPath, ctx }) : /* @__PURE__ */ jsx(BlogIndexView, { title, first, blogPath, ctx }) }) });
3801
3808
  }
3802
- function SettoBlog({ basePath = BLOG_ROUTE, title }) {
3809
+ function SettoBlog({
3810
+ basePath = BLOG_ROUTE,
3811
+ title,
3812
+ path,
3813
+ initialPost
3814
+ }) {
3803
3815
  useBlogStyles();
3804
- const ctx = useBlogContext();
3816
+ const ctx = useBlogContext(path);
3805
3817
  const base = normalisePath(basePath);
3806
3818
  const here = normalisePath(ctx.path);
3807
3819
  const rest = here.startsWith(`${base}/`) ? here.slice(base.length + 1) : "";
3808
3820
  const slug = rest && isValidBlogSlug(rest) ? rest : null;
3809
3821
  const inner = useRef(null);
3810
3822
  const clear = useClearFixedHeader(inner, true);
3811
- return /* @__PURE__ */ jsx("section", { className: "setto-blog", children: /* @__PURE__ */ jsx("div", { className: "setto-blog__inner", ref: inner, style: clear ? { paddingTop: clear } : void 0, children: slug ? /* @__PURE__ */ jsx(BlogPostView, { slug, blogPath: base, ctx }) : /* @__PURE__ */ jsx(BlogIndexView, { title: title ?? ctx.strings.blog, first: true, blogPath: base, ctx }) }) });
3823
+ return /* @__PURE__ */ jsx("section", { className: "setto-blog", children: /* @__PURE__ */ jsx("div", { className: "setto-blog__inner", ref: inner, style: clear ? { paddingTop: clear } : void 0, children: slug ? /* @__PURE__ */ jsx(BlogPostView, { slug, blogPath: base, ctx, initialPost }) : /* @__PURE__ */ jsx(BlogIndexView, { title: title ?? ctx.strings.blog, first: true, blogPath: base, ctx }) }) });
3812
3824
  }
3813
3825
  function useClearFixedHeader(ref, enabled) {
3814
3826
  const [clear, setClear] = useState(0);
@@ -3868,16 +3880,31 @@ function PostCard({
3868
3880
  ] })
3869
3881
  ] });
3870
3882
  }
3871
- function BlogPostView({ slug, blogPath, ctx }) {
3883
+ function isPostFor(post, summary) {
3884
+ return !!post && !!summary && post.frontmatter.slug === summary.slug && post.frontmatter.locale === summary.locale;
3885
+ }
3886
+ function BlogPostView({
3887
+ slug,
3888
+ blogPath,
3889
+ ctx,
3890
+ initialPost
3891
+ }) {
3872
3892
  const blog = ctx.config.blog;
3873
3893
  const summary = blog ? postForSlug(blog.posts, slug, ctx.language, ctx.fallbackLanguage) : null;
3874
- const [post, setPost] = useState(null);
3875
- const [state, setState] = useState(summary ? "loading" : "missing");
3894
+ const [post, setPost] = useState(() => isPostFor(initialPost, summary) ? initialPost : null);
3895
+ const [state, setState] = useState(
3896
+ () => !summary ? "missing" : isPostFor(initialPost, summary) ? "ready" : "loading"
3897
+ );
3876
3898
  useEffect(() => {
3877
3899
  if (!blog || !summary) {
3878
3900
  setState("missing");
3879
3901
  return;
3880
3902
  }
3903
+ if (isPostFor(initialPost, summary)) {
3904
+ setPost(initialPost);
3905
+ setState("ready");
3906
+ return;
3907
+ }
3881
3908
  let live = true;
3882
3909
  setState("loading");
3883
3910
  blog.load(summary.slug, summary.locale).then((loaded) => {
@@ -3902,7 +3929,7 @@ function BlogPostView({ slug, blogPath, ctx }) {
3902
3929
  ] });
3903
3930
  }
3904
3931
  const fm = post?.frontmatter ?? summary;
3905
- return /* @__PURE__ */ jsx(PostArticle, { frontmatter: fm, ctx, before: back, after: /* @__PURE__ */ jsx(ShareBar, { title: fm.title, strings: ctx.strings }), children: state === "ready" && post ? /* @__PURE__ */ jsx(BlogBody, { nodes: post.body.children, language: ctx.language }) : /* @__PURE__ */ jsx("p", { className: "setto-blog__loading", "aria-live": "polite", children: ctx.strings.loading }) });
3932
+ return /* @__PURE__ */ jsx(PostArticle, { frontmatter: fm, ctx, before: back, after: /* @__PURE__ */ jsx(ShareBar, { title: fm.title, strings: ctx.strings }), children: state === "ready" && isPostFor(post, summary) ? /* @__PURE__ */ jsx(BlogBody, { nodes: post.body.children, language: ctx.language }) : /* @__PURE__ */ jsx("p", { className: "setto-blog__loading", "aria-live": "polite", children: ctx.strings.loading }) });
3906
3933
  }
3907
3934
  function PostArticle({
3908
3935
  frontmatter: fm,
@@ -3947,8 +3974,11 @@ function usePostTitle(title, siteName) {
3947
3974
  function ShareBar({ title, strings }) {
3948
3975
  const [copied, setCopied] = useState(false);
3949
3976
  const [native, setNative] = useState(false);
3950
- useEffect(() => setNative(typeof navigator !== "undefined" && typeof navigator.share === "function"), []);
3951
- const url = typeof window === "undefined" ? "" : `${window.location.origin}${window.location.pathname}`;
3977
+ const [url, setUrl] = useState("");
3978
+ useEffect(() => {
3979
+ setNative(typeof navigator !== "undefined" && typeof navigator.share === "function");
3980
+ setUrl(`${window.location.origin}${window.location.pathname}`);
3981
+ }, []);
3952
3982
  const u = encodeURIComponent(url);
3953
3983
  const t = encodeURIComponent(title);
3954
3984
  return /* @__PURE__ */ jsxs("div", { className: "setto-blog__share", "aria-label": strings.share, children: [