AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

announcement-bar.tsx 3.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import { ChevronLeft, ChevronRight } from 'lucide-react';
  2. import { useEffect, useState } from 'react';
  3. import { useStorefront } from '../context';
  4. import { storefrontText } from '../text';
  5. /** How long each message stays, in milliseconds. */
  6. const ROTATE_EVERY = 4500;
  7. /**
  8. * The slim bar above the header: free shipping, running automatic discounts and the merchant's own lines.
  9. * Several messages take turns. They stop moving while the shopper points at the bar or uses the arrows,
  10. * and never move for shoppers whose device asks for less motion.
  11. */
  12. export function AnnouncementBar() {
  13. const { shop, language } = useStorefront();
  14. const t = storefrontText(language);
  15. const messages = shop.announcements;
  16. const [index, setIndex] = useState(0);
  17. const [paused, setPaused] = useState(false);
  18. useEffect(() => {
  19. const calm = window.matchMedia(
  20. '(prefers-reduced-motion: reduce)',
  21. ).matches;
  22. if (messages.length < 2 || paused || calm) {
  23. return;
  24. }
  25. const timer = setInterval(
  26. () => setIndex((current) => (current + 1) % messages.length),
  27. ROTATE_EVERY,
  28. );
  29. return () => clearInterval(timer);
  30. }, [messages.length, paused]);
  31. if (messages.length === 0) {
  32. return null;
  33. }
  34. const current = messages[index % messages.length];
  35. const step = (by: number) => {
  36. setPaused(true);
  37. setIndex((value) => (value + by + messages.length) % messages.length);
  38. };
  39. return (
  40. <div
  41. className="bg-(--sf-primary) text-(--sf-primary-fg)"
  42. data-test="announcement-bar"
  43. onMouseEnter={() => setPaused(true)}
  44. onMouseLeave={() => setPaused(false)}
  45. >
  46. <div className="mx-auto flex max-w-6xl items-center justify-between gap-2 px-3 py-2 text-center text-xs font-medium sm:text-sm">
  47. {messages.length > 1 ? (
  48. <button
  49. type="button"
  50. onClick={() => step(-1)}
  51. aria-label={t.previousMessage}
  52. className="rounded p-1 opacity-80 hover:opacity-100 focus-visible:outline-2 focus-visible:outline-current"
  53. >
  54. <ChevronLeft className="size-4" aria-hidden="true" />
  55. </button>
  56. ) : (
  57. <span className="w-6" aria-hidden="true" />
  58. )}
  59. <p key={index} className="sf-fade-in min-w-0 flex-1 truncate">
  60. {current}
  61. </p>
  62. {messages.length > 1 ? (
  63. <button
  64. type="button"
  65. onClick={() => step(1)}
  66. aria-label={t.nextMessage}
  67. className="rounded p-1 opacity-80 hover:opacity-100 focus-visible:outline-2 focus-visible:outline-current"
  68. >
  69. <ChevronRight className="size-4" aria-hidden="true" />
  70. </button>
  71. ) : (
  72. <span className="w-6" aria-hidden="true" />
  73. )}
  74. </div>
  75. </div>
  76. );
  77. }