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" />
	)
}

效果图

相关推荐
恋猫de小郭11 分钟前
Android Studio Cloud 正式上线,不只是 Android,随时随地改 bug
android·前端·flutter
清岚_lxn5 小时前
原生SSE实现AI智能问答+Vue3前端打字机流效果
前端·javascript·人工智能·vue·ai问答
ZoeLandia5 小时前
Element UI 设置 el-table-column 宽度 width 为百分比无效
前端·ui·element-ui
橘子味的冰淇淋~6 小时前
解决 vite.config.ts 引入scss 预处理报错
前端·vue·scss
小小小小宇7 小时前
V8 引擎垃圾回收机制详解
前端
lauo8 小时前
智体知识库:ai-docs对分布式智体编程语言Poplang和javascript的语法的比较(知识库问答)
开发语言·前端·javascript·分布式·机器人·开源
拉不动的猪8 小时前
设计模式之------单例模式
前端·javascript·面试
一袋米扛几楼988 小时前
【React框架】什么是 Vite?如何使用vite自动生成react的目录?
前端·react.js·前端框架
Alt.98 小时前
SpringMVC基础二(RestFul、接收数据、视图跳转)
java·开发语言·前端·mvc
进取星辰9 小时前
1、从零搭建魔法工坊:React 19 新手村生存指南
前端·react.js·前端框架