RN封装三角形组件(只支持上下箭头)

js 复制代码
import React from 'react';
import { View, StyleSheet } from 'react-native';

const Triangle = ({ direction, width, height, color }) => {
  // 根据方向选择三角形的样式
  const triangleStyle =
    direction === 'up'
      ? {
          borderTopWidth: 0,
          borderBottomWidth: height,
          borderLeftWidth: width / 2,
          borderRightWidth: width / 2,
          borderTopColor: 'transparent',
          borderBottomColor: color,
          borderLeftColor: 'transparent',
          borderRightColor: 'transparent',
        }
      : {
          borderTopWidth: height,
          borderBottomWidth: 0,
          borderLeftWidth: width / 2,
          borderRightWidth: width / 2,
          borderTopColor: color,
          borderBottomColor: 'transparent',
          borderLeftColor: 'transparent',
          borderRightColor: 'transparent',
        };

  return <View style={[styles.triangle, triangleStyle]} />;
};

const styles = StyleSheet.create({
  triangle: {
    width: 0,
    height: 0,
    backgroundColor: 'transparent',
    borderStyle: 'solid',
  },
});

export default Triangle;

direction(方向)、size(大小)和 color(颜色)作为属性来指定三角形的样式参数 使用

js 复制代码
import Triangle from './uitl.js'

export default function App(){
	return(
		//红色向上箭头,尺寸20
		<Triangle direction="up" width={20} height={10} color="red" />
		// 蓝色向下箭头,尺寸20
		<Triangle direction="down" width={20} height={10} color="blue" />
	)
}

效果图

相关推荐
步步为营DotNet27 分钟前
深度解析CancellationToken:.NET中的优雅取消机制
java·前端·.net
一位搞嵌入式的 genius2 小时前
从 ES6 到 ESNext:JavaScript 现代语法全解析(含编译工具与实战)
前端·javascript·ecmascript·es6
linweidong4 小时前
C++ 模块化编程(Modules)在大规模系统中的实践难点?
linux·前端·c++
leobertlan7 小时前
2025年终总结
前端·后端·程序员
子兮曰7 小时前
OpenClaw架构揭秘:178k stars的个人AI助手如何用Gateway模式统一控制12+通讯频道
前端·javascript·github
百锦再8 小时前
Reactive编程入门:Project Reactor 深度指南
前端·javascript·python·react.js·django·前端框架·reactjs
莲华君8 小时前
React快速上手:从零到项目实战
前端·reactjs教程
百锦再8 小时前
React编程高级主题:测试代码
android·前端·javascript·react.js·前端框架·reactjs
易安说AI8 小时前
Ralph Loop 让Claude无止尽干活的牛马...
前端·后端
失忆爆表症10 小时前
05_UI 组件库集成指南:Shadcn/ui + Tailwind CSS v4
前端·css·ui