Browse Source

[doc] 更新Clis组件,添加打字效果功能,优化代码显示逻辑,并调整样式以提升用户体验。

dufu1991 1 year ago
parent
commit
b742b3eeae
2 changed files with 31 additions and 23 deletions
  1. 31 18
      docs/site/src/lib/clis/Clis.svelte
  2. 0 5
      docs/site/src/utils/code-group-svg-data.js

+ 31 - 18
docs/site/src/lib/clis/Clis.svelte

@@ -3,56 +3,69 @@
 	import codeGroupSvgData from '../../utils/code-group-svg-data';
 	const isZh = localStorage.getItem('lang') === 'zh_CN';
 
+	// 将 codeGroupSvgData 随机打乱
+	const randomCodeGroupSvgData = codeGroupSvgData.sort(() => Math.random() - 0.5);
+
 	let showCli = $state(0);
 	let intervalTime: ReturnType<typeof setInterval>;
-	let animation = $state(true);
 	let isClicked = $state(false);
 	let isHover = $state(false);
+	let textCli = $state('_');
+	let times: ReturnType<typeof setInterval>;
+	// 写一个函数,模拟打字效果,传入一段文字,返回一个打字效果的函数
+	const typeWriter = (text: string, speed: number = 150) => {
+		let number = 0;
+		times = setInterval(() => {
+			number++;
+			if (number >= text.length) {
+				number = 0;
+				textCli = text;
+				clearInterval(times);
+			} else {
+				textCli = text.slice(0, number) + '_';
+			}
+		}, speed);
+	};
+
 	// 每 3 秒切换一次
 	onMount(() => {
+		typeWriter(randomCodeGroupSvgData[showCli].cli);
 		intervalTime = setInterval(() => {
-			// 当是最后一个时立马切换到第一个,并且动画结束
 			if (showCli === codeGroupSvgData.length - 1) {
 				showCli = 0;
-				animation = false;
 			} else {
-				animation = true;
 				showCli++;
 			}
+			textCli = '_';
+			setTimeout(() => {
+				typeWriter(randomCodeGroupSvgData[showCli].cli);
+			}, 300);
 		}, 6000);
 	});
 	onDestroy(() => {
 		clearInterval(intervalTime);
+		clearInterval(times);
 	});
 </script>
 
 <div class="inline-flex justify-center">
-	<div
-		class="w-18 relative overflow-hidden rounded-l-lg border border-black/5 border-r-transparent bg-black/5 text-xs dark:border-white/10 dark:border-r-transparent dark:bg-white/10"
-	>
-		<div class="absolute {animation ? 'transition-all duration-300' : ''}" style="top: -{showCli * 28}px">
-			{#each codeGroupSvgData as cli}
-				<div class="flex h-7 items-center gap-1 px-3">
-					<span class="translate-y-px">{@html cli.svg}</span>
-					{cli.name}
-				</div>
-			{/each}
-		</div>
+	<div class="bg-primary/5 dark:bg-dark/10 rounded-l-full text-xs">
+		<div class="mr-2 flex h-7 items-center gap-1 pl-3">{@html randomCodeGroupSvgData[showCli].svg}</div>
 	</div>
 	<button
 		type="button"
 		onmouseenter={() => (isHover = true)}
 		onmouseleave={() => (isHover = false)}
-		class="relative inline-flex cursor-copy items-center justify-center rounded-r-lg border border-black/5 bg-black/5 px-4 py-1 transition-all duration-300 dark:border-white/10 dark:bg-white/10"
+		class="bg-primary/5 dark:bg-dark/10 relative inline-flex cursor-copy items-center justify-center rounded-r-full py-1 pr-3"
 		onclick={async () => {
-			await navigator.clipboard.writeText(codeGroupSvgData[showCli].cli);
+			await navigator.clipboard.writeText(randomCodeGroupSvgData[showCli].cli);
 			isClicked = true;
 			setTimeout(() => {
 				isClicked = false;
 			}, 2000);
 		}}
 	>
-		<code class="text-sm opacity-80">{codeGroupSvgData[showCli].cli}</code>
+		<code class="text-primary dark:text-dark text-sm opacity-80">{textCli}</code>
 		<div
 			class="bg-primary dark:bg-dark rounded-xs absolute -top-3 left-1/2 size-3 -translate-x-1/2 rotate-45 transition-all {isHover
 				? '-translate-y-1/2 opacity-100'

+ 0 - 5
docs/site/src/utils/code-group-svg-data.js

@@ -18,10 +18,5 @@ export default [
 		name: 'yarn',
 		cli: 'yarn create stdf',
 		svg: '<svg xmlns="http://www.w3.org/2000/svg" width="1em" height="1em" viewBox="0 0 32 32"><path fill="#2188b6" d="M28.208 24.409a10.5 10.5 0 0 0-3.959 1.822a23.7 23.7 0 0 1-5.835 2.642a1.63 1.63 0 0 1-.983.55a62 62 0 0 1-6.447.577c-1.163.009-1.876-.3-2.074-.776a1.573 1.573 0 0 1 .866-2.074a4 4 0 0 1-.514-.379c-.171-.171-.352-.514-.406-.388c-.225.55-.343 1.894-.947 2.5c-.83.839-2.4.559-3.328.072c-1.019-.541.072-1.813.072-1.813a.73.73 0 0 1-.992-.343a4.85 4.85 0 0 1-.667-2.949a5.37 5.37 0 0 1 1.749-2.895a9.3 9.3 0 0 1 .658-4.4a10.45 10.45 0 0 1 3.165-3.661S6.628 10.747 7.35 8.817c.469-1.262.658-1.253.812-1.308a3.6 3.6 0 0 0 1.452-.857a5.27 5.27 0 0 1 4.41-1.7S15.2 1.4 16.277 2.09a18.4 18.4 0 0 1 1.533 2.886s1.281-.748 1.425-.469a11.33 11.33 0 0 1 .523 6.132a14 14 0 0 1-2.6 5.411c-.135.225 1.551.938 2.615 3.887c.983 2.7.108 4.96.262 5.212c.027.045.036.063.036.063s1.127.09 3.391-1.308a8.5 8.5 0 0 1 4.277-1.604a1.081 1.081 0 0 1 .469 2.11Z"/></svg>'
-	},
-	{
-		name: 'pnpm',
-		cli: 'pnpm create stdf',
-		svg: '<svg xmlns="http://www.w3.org/2000/svg" width="1em" height="1em" viewBox="0 0 32 32"><path fill="#f9ad00" d="M30 10.75h-8.749V2H30Zm-9.626 0h-8.75V2h8.75Zm-9.625 0H2V2h8.749ZM30 20.375h-8.749v-8.75H30Z"/><path fill="#4e4e4e" d="M20.374 20.375h-8.75v-8.75h8.75Zm0 9.625h-8.75v-8.75h8.75ZM30 30h-8.749v-8.75H30Zm-19.251 0H2v-8.75h8.749Z"/></svg>'
 	}
 ];