AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import type { CSSProperties } from 'react';
  2. import type { FontMap, Theme, ThemeRadius, ThemeStyle } from './types';
  3. /** Keep in step with PlanSchema::STYLE_DEFAULTS. They look like stores did before styles existed. */
  4. export const STYLE_DEFAULTS: ThemeStyle = {
  5. buttons: 'solid',
  6. cards: 'border',
  7. spacing: 'normal',
  8. headings: 'normal',
  9. backgrounds: 'plain',
  10. motion: 'none',
  11. };
  12. /** The theme's style with every part filled in. */
  13. export function styleOf(theme: Theme): ThemeStyle {
  14. return { ...STYLE_DEFAULTS, ...theme.style };
  15. }
  16. /**
  17. * Data attributes for the storefront's root element. app.css reads them, so a style changes
  18. * every button, card and section at once without each component knowing about it.
  19. */
  20. export function styleAttributes(theme: Theme): Record<string, string> {
  21. const style = styleOf(theme);
  22. return {
  23. 'data-buttons': style.buttons,
  24. 'data-cards': style.cards,
  25. 'data-spacing': style.spacing,
  26. 'data-headings': style.headings,
  27. 'data-backgrounds': style.backgrounds,
  28. 'data-motion': style.motion,
  29. };
  30. }
  31. const buttonRadius: Record<ThemeRadius, string> = {
  32. none: '0px',
  33. small: '4px',
  34. medium: '10px',
  35. large: '16px',
  36. full: '9999px',
  37. };
  38. const cardRadius: Record<ThemeRadius, string> = {
  39. none: '0px',
  40. small: '6px',
  41. medium: '14px',
  42. large: '22px',
  43. full: '28px',
  44. };
  45. const fallbackFont = 'ui-sans-serif, system-ui, sans-serif';
  46. function family(key: string, fonts: FontMap): string {
  47. const name = fonts[key];
  48. return name ? `'${name}', ${fallbackFont}` : fallbackFont;
  49. }
  50. /** Turn a plan theme into the CSS variables every section reads. */
  51. export function themeStyle(theme: Theme, fonts: FontMap): CSSProperties {
  52. return {
  53. '--sf-bg': theme.colors.background,
  54. '--sf-surface': theme.colors.surface,
  55. '--sf-fg': theme.colors.foreground,
  56. '--sf-muted': theme.colors.muted,
  57. '--sf-primary': theme.colors.primary,
  58. '--sf-primary-fg': theme.colors.primaryForeground,
  59. '--sf-accent': theme.colors.accent,
  60. '--sf-border': theme.colors.border,
  61. '--sf-radius': buttonRadius[theme.radius] ?? buttonRadius.medium,
  62. '--sf-radius-card': cardRadius[theme.radius] ?? cardRadius.medium,
  63. '--sf-font-heading': family(theme.fonts.heading, fonts),
  64. '--sf-font-body': family(theme.fonts.body, fonts),
  65. } as CSSProperties;
  66. }
  67. /** Web address that loads the theme's fonts. */
  68. export function fontStylesheetUrl(theme: Theme, fonts: FontMap): string | null {
  69. const keys = [...new Set([theme.fonts.heading, theme.fonts.body])].filter(
  70. (key) => key in fonts,
  71. );
  72. if (keys.length === 0) {
  73. return null;
  74. }
  75. const families = keys.map((key) => `${key}:400,500,600,700`).join('|');
  76. return `https://fonts.bunny.net/css?family=${families}&display=swap`;
  77. }
  78. const ringgit = new Intl.NumberFormat('en-MY', {
  79. style: 'currency',
  80. currency: 'MYR',
  81. });
  82. export function formatPrice(amount: number): string {
  83. return ringgit.format(amount);
  84. }
  85. /**
  86. * Put in front of a page title to say "this is a store's own page".
  87. * app.tsx removes it and leaves out our app name.
  88. */
  89. export const storefrontTitlePrefix = 'storefront::';