All files / src/components/FeedbackSection index.tsx

97.82% Statements 90/92
92.85% Branches 13/14
100% Functions 2/2
97.82% Lines 90/92

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133  1x     1x     1x     1x     1x     1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                     1x       1x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x     62x     62x 57x 57x   54x 54x 62x     62x 124x 124x 116x 124x     124x   62x 48x 62x   62x 62x 62x 62x   62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x   62x 62x 62x 62x 62x 62x 62x 62x   62x 62x 62x 62x 9x   9x 9x 62x 62x 62x 62x   62x 1x   1x  
// Libs
import { forwardRef, useCallback, useEffect, useRef } from 'react';
 
// Hooks
import { useAutoFocus } from '@/hooks';
 
// Types
import { BaseVariant } from '@/types';
 
// Utils
import { combineClasses } from '@shared/utils';
 
// Constants
import { MENU_FEEDBACK_LABELS } from '@/constants/dailyFeedback';
 
// Styles
import './styles/feedback-section.css';
 
const variantTheme = {
  [BaseVariant.PRIMARY]: {
    root: 'feedback-section-root',
    questionRow: 'feedback-section-question-row',
    question: 'feedback-section-question',
    toggle: 'feedback-section-toggle',
    toggleOn: 'feedback-section-toggle-on',
    toggleOff: 'feedback-section-toggle-off',
    toggleThumb: 'feedback-section-toggle-thumb',
    toggleThumbOn: 'feedback-section-toggle-thumb-on',
    toggleThumbOff: 'feedback-section-toggle-thumb-off',
    textarea: 'feedback-section-textarea',
  },
};
 
export interface FeedbackSectionProps {
  comment: string;
  onCommentChange: (value: string) => void;
  isAnonymous?: boolean;
  onToggleAnonymous?: () => void;
  className?: string;
  variant?: BaseVariant;
}
 
export const FeedbackSection = forwardRef<
  HTMLTextAreaElement,
  FeedbackSectionProps
>(
  (
    {
      comment,
      onCommentChange,
      isAnonymous = false,
      onToggleAnonymous,
      className = '',
      variant = BaseVariant.PRIMARY,
    },
    ref,
  ) => {
    const theme = variantTheme[variant];
    const textareaRef = useRef<HTMLTextAreaElement>(null);
 
    // Auto-focus textarea with retry logic for mobile animations
    useAutoFocus(textareaRef);
 
    // Auto-resize textarea to fit content: reset height first to get accurate scrollHeight
    const resizeTextarea = useCallback(() => {
      const textarea = textareaRef.current;
      if (!textarea) return;
 
      textarea.style.height = 'auto';
      textarea.style.height = `${textarea.scrollHeight}px`;
    }, []);
 
    // Sync both internal ref (for resizing) and forwarded ref (for parent access)
    const handleTextareaRef = (el: HTMLTextAreaElement | null) => {
      textareaRef.current = el;
      if (typeof ref === 'function') {
        ref(el);
      } else if (ref) {
        ref.current = el;
      }
    };
 
    useEffect(() => {
      resizeTextarea();
    }, [comment, resizeTextarea]);
 
    return (
      <div
        data-variant={variant}
        className={combineClasses(theme.root, className)}
      >
        <div className={theme.questionRow}>
          <span className={theme.question}>
            {MENU_FEEDBACK_LABELS.question}
          </span>
          <button
            type="button"
            role="switch"
            aria-checked={isAnonymous}
            aria-label={MENU_FEEDBACK_LABELS.toggleAnonymous}
            onClick={onToggleAnonymous}
            className={combineClasses(
              theme.toggle,
              isAnonymous ? theme.toggleOn : theme.toggleOff,
            )}
          >
            <span
              className={combineClasses(
                theme.toggleThumb,
                isAnonymous ? theme.toggleThumbOn : theme.toggleThumbOff,
              )}
            />
          </button>
        </div>
 
        <textarea
          ref={handleTextareaRef}
          value={comment}
          onChange={(e) => {
            onCommentChange(e.target.value);
            // Defer resize to next frame after DOM update for accurate scrollHeight
            requestAnimationFrame(resizeTextarea);
          }}
          placeholder={MENU_FEEDBACK_LABELS.placeholder}
          className={theme.textarea}
        />
      </div>
    );
  },
);
 
FeedbackSection.displayName = 'FeedbackSection';