@sevenfold/setto-client 0.48.0 → 0.50.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 {
@@ -0,0 +1,36 @@
1
+ /**
2
+ * A page's own title and description, shared by the build (`settoBlog()`
3
+ * writes `<route>/index.html` with them) and the browser (`SettoPage` sets
4
+ * `document.title`), so the head a crawler reads without JavaScript and the
5
+ * one it reads after rendering are the same answer.
6
+ *
7
+ * Pure and dependency-free: the browser bundle and the Node plugin both
8
+ * import it.
9
+ */
10
+ /**
11
+ * The `<meta name>` the build writes on every page it gives a head of its
12
+ * own: the front page's title, which a page's title is built on. Without it
13
+ * the browser could only take `document.title` as the base, which on such a
14
+ * page is already the page's own title, and «Priser · Priser · …» follows.
15
+ */
16
+ export declare const TITLE_BASE_META = "setto:title-base";
17
+ /** A run of four or more capitals and not one small letter: «DIN FRISØR AS». */
18
+ export declare const isAllCaps: (value: string) => boolean;
19
+ /** «Nordvik Rør» from «Nordvik Rør – rørlegger i Ålesund». */
20
+ export declare function nameFromTitle(title: string | undefined): string | undefined;
21
+ /**
22
+ * A page's title from its own (`pages.priser`: «Priser») and the front
23
+ * page's («Sportsparken Vegsund – aktivitetspark i Ålesund»):
24
+ * «Priser · Sportsparken Vegsund – aktivitetspark i Ålesund».
25
+ *
26
+ * A page title that already names the business («Prisar – Hundeidvik
27
+ * Familiepark») is a whole title and is used as it is.
28
+ */
29
+ export declare function pageTitle(title: string | undefined | null, base: string | undefined | null): string;
30
+ /**
31
+ * Where a page's description lives: beside a title key that ends in
32
+ * `.title` (`pages.prisar.title` → `pages.prisar.description`). A title key
33
+ * that is a string of its own (`pages.priser`) has nowhere beside it, and the
34
+ * page takes the site's description.
35
+ */
36
+ export declare function descriptionKeyFor(titleKey: string): string | null;
@@ -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();
@@ -3310,6 +3316,23 @@ const removeBtnStyle = {
3310
3316
  boxShadow: "0 1px 4px rgba(0,0,0,0.18)",
3311
3317
  fontFamily: 'system-ui, -apple-system, "Segoe UI", sans-serif'
3312
3318
  };
3319
+ const TITLE_BASE_META = "setto:title-base";
3320
+ const isAllCaps = (value) => !/\p{Ll}/u.test(value) && /\p{Lu}{4,}/u.test(value);
3321
+ function nameFromTitle(title) {
3322
+ if (!title) return void 0;
3323
+ const separator = /\s(?:–|—|\||-)\s/.exec(title);
3324
+ const name = (separator ? title.slice(0, separator.index) : title).replace(/\s+/g, " ").trim();
3325
+ return name && !isAllCaps(name) ? name : void 0;
3326
+ }
3327
+ function pageTitle(title, base) {
3328
+ const own = title?.replace(/\s+/g, " ").trim() ?? "";
3329
+ const front = base?.replace(/\s+/g, " ").trim() ?? "";
3330
+ if (!own) return front;
3331
+ if (!front || own === front) return own;
3332
+ const name = nameFromTitle(front);
3333
+ if (name && own.toLowerCase().includes(name.toLowerCase())) return own;
3334
+ return `${own} · ${front}`;
3335
+ }
3313
3336
  function usePageManifest() {
3314
3337
  const { pageStore, config } = useSetto();
3315
3338
  const snap = useSyncExternalStore(
@@ -3456,9 +3479,9 @@ function usePageTitle(page) {
3456
3479
  const title = page.path === "/" ? null : t(page.titleKey);
3457
3480
  useEffect(() => {
3458
3481
  if (!title || typeof document === "undefined") return;
3459
- original.current ??= document.title;
3482
+ original.current ??= document.querySelector(`meta[name="${TITLE_BASE_META}"]`)?.content || document.title;
3460
3483
  const base = original.current;
3461
- document.title = base ? `${title} · ${base}` : title;
3484
+ document.title = pageTitle(title, base);
3462
3485
  return () => {
3463
3486
  document.title = base;
3464
3487
  };
@@ -3775,15 +3798,16 @@ function YouTubeEmbed({ id, title, language }) {
3775
3798
  ] });
3776
3799
  }
3777
3800
  const LATEST_COUNT = 3;
3778
- function useBlogContext() {
3801
+ function useBlogContext(path) {
3779
3802
  const { config } = useSetto();
3780
3803
  const { i18n, t } = useTranslation();
3781
3804
  const manifest = usePageManifest();
3782
3805
  const language = i18n.language || config.languages?.[0] || "no";
3783
3806
  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 };
3807
+ const languages = config.languages ?? [fallbackLanguage];
3808
+ const route = typeof window === "undefined" ? { basename: language === fallbackLanguage || !languages.includes(language) ? "" : `/${language}`, rest: "/" } : languageFromPath(window.location.pathname, languages);
3809
+ const href = (to) => `${route.basename}${to}`;
3810
+ return { config, t, manifest, language, fallbackLanguage, strings: blogStrings(language), path: path ?? route.rest, href };
3787
3811
  }
3788
3812
  function blogPathOf(manifest) {
3789
3813
  return manifest && blogPage(manifest)?.page.path || BLOG_ROUTE;
@@ -3799,16 +3823,21 @@ function SettoBlogSection({ sectionId, first }) {
3799
3823
  const clear = useClearFixedHeader(inner, first);
3800
3824
  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
3825
  }
3802
- function SettoBlog({ basePath = BLOG_ROUTE, title }) {
3826
+ function SettoBlog({
3827
+ basePath = BLOG_ROUTE,
3828
+ title,
3829
+ path,
3830
+ initialPost
3831
+ }) {
3803
3832
  useBlogStyles();
3804
- const ctx = useBlogContext();
3833
+ const ctx = useBlogContext(path);
3805
3834
  const base = normalisePath(basePath);
3806
3835
  const here = normalisePath(ctx.path);
3807
3836
  const rest = here.startsWith(`${base}/`) ? here.slice(base.length + 1) : "";
3808
3837
  const slug = rest && isValidBlogSlug(rest) ? rest : null;
3809
3838
  const inner = useRef(null);
3810
3839
  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 }) }) });
3840
+ 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
3841
  }
3813
3842
  function useClearFixedHeader(ref, enabled) {
3814
3843
  const [clear, setClear] = useState(0);
@@ -3868,16 +3897,31 @@ function PostCard({
3868
3897
  ] })
3869
3898
  ] });
3870
3899
  }
3871
- function BlogPostView({ slug, blogPath, ctx }) {
3900
+ function isPostFor(post, summary) {
3901
+ return !!post && !!summary && post.frontmatter.slug === summary.slug && post.frontmatter.locale === summary.locale;
3902
+ }
3903
+ function BlogPostView({
3904
+ slug,
3905
+ blogPath,
3906
+ ctx,
3907
+ initialPost
3908
+ }) {
3872
3909
  const blog = ctx.config.blog;
3873
3910
  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");
3911
+ const [post, setPost] = useState(() => isPostFor(initialPost, summary) ? initialPost : null);
3912
+ const [state, setState] = useState(
3913
+ () => !summary ? "missing" : isPostFor(initialPost, summary) ? "ready" : "loading"
3914
+ );
3876
3915
  useEffect(() => {
3877
3916
  if (!blog || !summary) {
3878
3917
  setState("missing");
3879
3918
  return;
3880
3919
  }
3920
+ if (isPostFor(initialPost, summary)) {
3921
+ setPost(initialPost);
3922
+ setState("ready");
3923
+ return;
3924
+ }
3881
3925
  let live = true;
3882
3926
  setState("loading");
3883
3927
  blog.load(summary.slug, summary.locale).then((loaded) => {
@@ -3902,7 +3946,7 @@ function BlogPostView({ slug, blogPath, ctx }) {
3902
3946
  ] });
3903
3947
  }
3904
3948
  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 }) });
3949
+ 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
3950
  }
3907
3951
  function PostArticle({
3908
3952
  frontmatter: fm,
@@ -3947,8 +3991,11 @@ function usePostTitle(title, siteName) {
3947
3991
  function ShareBar({ title, strings }) {
3948
3992
  const [copied, setCopied] = useState(false);
3949
3993
  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}`;
3994
+ const [url, setUrl] = useState("");
3995
+ useEffect(() => {
3996
+ setNative(typeof navigator !== "undefined" && typeof navigator.share === "function");
3997
+ setUrl(`${window.location.origin}${window.location.pathname}`);
3998
+ }, []);
3952
3999
  const u = encodeURIComponent(url);
3953
4000
  const t = encodeURIComponent(title);
3954
4001
  return /* @__PURE__ */ jsxs("div", { className: "setto-blog__share", "aria-label": strings.share, children: [