|
| 1 | +import React from 'react'; |
| 2 | +import { |
| 3 | + OnboardingProvider, |
| 4 | + useOnboarding, |
| 5 | +} from '@/contexts/OnboardingContext'; |
| 6 | +import { getOnboardingStepsForPage } from '@/constants/OnboardingSteps'; |
| 7 | + |
| 8 | +interface OnboardingProps { |
| 9 | + children: React.ReactNode; |
| 10 | + pageType?: 'homepage' | 'try' | 'about' | 'news'; |
| 11 | + showProgressIndicator?: boolean; |
| 12 | + customSteps?: any[]; |
| 13 | +} |
| 14 | + |
| 15 | +export const Onboarding: React.FC<OnboardingProps> = ({ |
| 16 | + children, |
| 17 | + pageType = 'homepage', |
| 18 | + showProgressIndicator = true, |
| 19 | + customSteps, |
| 20 | +}) => { |
| 21 | + const steps = customSteps || getOnboardingStepsForPage(pageType); |
| 22 | + |
| 23 | + return ( |
| 24 | + <OnboardingProvider steps={steps}> |
| 25 | + <OnboardingContent showProgressIndicator={showProgressIndicator}> |
| 26 | + {children} |
| 27 | + </OnboardingContent> |
| 28 | + </OnboardingProvider> |
| 29 | + ); |
| 30 | +}; |
| 31 | + |
| 32 | +const OnboardingContent: React.FC<{ |
| 33 | + children: React.ReactNode; |
| 34 | + showProgressIndicator: boolean; |
| 35 | +}> = ({ children, showProgressIndicator }) => { |
| 36 | + const { isOnboardingActive, onboardingProgress, resetOnboarding } = |
| 37 | + useOnboarding(); |
| 38 | + |
| 39 | + return ( |
| 40 | + <div className="relative"> |
| 41 | + {children} |
| 42 | + |
| 43 | + {/* Optional Progress Indicator */} |
| 44 | + {showProgressIndicator && isOnboardingActive && ( |
| 45 | + <div className="fixed top-4 right-4 z-[9999] bg-white rounded-lg shadow-lg border p-3 min-w-[200px]"> |
| 46 | + <div className="flex items-center justify-between mb-2"> |
| 47 | + <span className="text-sm font-medium text-gray-700"> |
| 48 | + Tour Progress |
| 49 | + </span> |
| 50 | + <button |
| 51 | + onClick={resetOnboarding} |
| 52 | + className="text-xs text-gray-500 hover:text-gray-700 underline" |
| 53 | + title="Reset tour progress" |
| 54 | + > |
| 55 | + Reset |
| 56 | + </button> |
| 57 | + </div> |
| 58 | + <div className="w-full bg-gray-200 rounded-full h-2"> |
| 59 | + <div |
| 60 | + className="bg-blue-600 h-2 rounded-full transition-all duration-300 ease-out" |
| 61 | + style={{ width: `${onboardingProgress}%` }} |
| 62 | + /> |
| 63 | + </div> |
| 64 | + <div className="text-xs text-gray-500 mt-1 text-center"> |
| 65 | + {Math.round(onboardingProgress)}% complete |
| 66 | + </div> |
| 67 | + </div> |
| 68 | + )} |
| 69 | + </div> |
| 70 | + ); |
| 71 | +}; |
| 72 | + |
| 73 | +// useOnboarding hook is imported above |
| 74 | + |
| 75 | +// Add data attributes to elements for targeting |
| 76 | +export const addOnboardingIds = { |
| 77 | + sugarLabsLogo: () => ({ |
| 78 | + 'data-onboarding-id': 'sugar-labs-logo', |
| 79 | + className: 'sugar-labs-logo', |
| 80 | + }), |
| 81 | + mainNavigation: () => ({ |
| 82 | + 'data-onboarding-id': 'main-navigation', |
| 83 | + className: 'main-navigation', |
| 84 | + }), |
| 85 | + trySugarButton: () => ({ |
| 86 | + 'data-onboarding-id': 'try-sugar-button', |
| 87 | + className: 'try-sugar-button', |
| 88 | + }), |
| 89 | + activitiesSection: () => ({ |
| 90 | + 'data-onboarding-id': 'activities-section', |
| 91 | + className: 'activities-section', |
| 92 | + }), |
| 93 | + statsSection: () => ({ |
| 94 | + 'data-onboarding-id': 'stats-section', |
| 95 | + className: 'stats-section', |
| 96 | + }), |
| 97 | + donationSection: () => ({ |
| 98 | + 'data-onboarding-id': 'donation-section', |
| 99 | + className: 'donation-section', |
| 100 | + }), |
| 101 | +}; |
| 102 | + |
| 103 | +// Component to add onboarding trigger button |
| 104 | +export const OnboardingTrigger: React.FC<{ |
| 105 | + className?: string; |
| 106 | + children?: React.ReactNode; |
| 107 | +}> = ({ className = '', children = 'Start Tour' }) => { |
| 108 | + const { startOnboarding, onboardingCompleted } = useOnboarding(); |
| 109 | + |
| 110 | + if (onboardingCompleted) { |
| 111 | + return null; // Hide trigger if onboarding is completed |
| 112 | + } |
| 113 | + |
| 114 | + return ( |
| 115 | + <button |
| 116 | + onClick={startOnboarding} |
| 117 | + className={`inline-flex items-center px-4 py-2 bg-blue-600 text-white text-sm font-medium rounded-md hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-blue-500 focus:ring-offset-2 transition-colors duration-200 ${className}`} |
| 118 | + title="Take a tour to learn about Sugar Labs features" |
| 119 | + > |
| 120 | + <div className="mr-2 text-lg">🎯</div> |
| 121 | + {children} |
| 122 | + </button> |
| 123 | + ); |
| 124 | +}; |
| 125 | + |
| 126 | +// Custom tooltip wrapper component for styled onboarding tooltips |
| 127 | +export const OnboardingTooltipWrapper: React.FC<{ |
| 128 | + title: string; |
| 129 | + content: React.ReactNode; |
| 130 | + icon?: string; |
| 131 | + accentColor?: 'blue' | 'green' | 'purple' | 'orange' | 'red'; |
| 132 | +}> = ({ title, content, icon = '✨', accentColor = 'blue' }) => { |
| 133 | + const colorClasses = { |
| 134 | + blue: 'from-blue-50 to-blue-100 border-blue-200', |
| 135 | + green: 'from-green-50 to-green-100 border-green-200', |
| 136 | + purple: 'from-purple-50 to-purple-100 border-purple-200', |
| 137 | + orange: 'from-orange-50 to-orange-100 border-orange-200', |
| 138 | + red: 'from-red-50 to-red-100 border-red-200', |
| 139 | + }; |
| 140 | + |
| 141 | + const iconClasses = { |
| 142 | + blue: 'text-blue-600', |
| 143 | + green: 'text-green-600', |
| 144 | + purple: 'text-purple-600', |
| 145 | + orange: 'text-orange-600', |
| 146 | + red: 'text-red-600', |
| 147 | + }; |
| 148 | + |
| 149 | + return ( |
| 150 | + <div |
| 151 | + className={`bg-gradient-to-br ${colorClasses[accentColor]} border rounded-xl p-6 shadow-lg`} |
| 152 | + > |
| 153 | + <div className="flex items-center mb-4"> |
| 154 | + <span className={`text-2xl mr-3 ${iconClasses[accentColor]}`}> |
| 155 | + {icon} |
| 156 | + </span> |
| 157 | + <h3 className="text-xl font-bold text-gray-800">{title}</h3> |
| 158 | + </div> |
| 159 | + <div className="text-gray-700">{content}</div> |
| 160 | + </div> |
| 161 | + ); |
| 162 | +}; |
| 163 | + |
| 164 | +// Helper component to render onboarding step content |
| 165 | +export const OnboardingStep: React.FC<{ |
| 166 | + title: string; |
| 167 | + description: string; |
| 168 | + features?: string[]; |
| 169 | + note?: string; |
| 170 | + accentColor?: 'blue' | 'green' | 'purple' | 'orange' | 'red'; |
| 171 | +}> = ({ title, description, features = [], note }) => { |
| 172 | + return ( |
| 173 | + <div className="space-y-4"> |
| 174 | + <div className="space-y-3"> |
| 175 | + <h3 className="text-xl font-semibold text-gray-800">{title}</h3> |
| 176 | + <p className="text-gray-600 leading-relaxed">{description}</p> |
| 177 | + |
| 178 | + {features.length > 0 && ( |
| 179 | + <ul className="space-y-2"> |
| 180 | + {features.map((feature, index) => ( |
| 181 | + <li key={index} className="flex items-start"> |
| 182 | + <span className="text-blue-500 mr-2 mt-1">•</span> |
| 183 | + <span className="text-gray-700">{feature}</span> |
| 184 | + </li> |
| 185 | + ))} |
| 186 | + </ul> |
| 187 | + )} |
| 188 | + |
| 189 | + {note && ( |
| 190 | + <div className="mt-4 p-3 bg-blue-50 rounded-lg border border-blue-200"> |
| 191 | + <p className="text-blue-800 font-medium text-sm">{note}</p> |
| 192 | + </div> |
| 193 | + )} |
| 194 | + </div> |
| 195 | + </div> |
| 196 | + ); |
| 197 | +}; |
| 198 | + |
| 199 | +export default Onboarding; |
0 commit comments