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

效果图

相关推荐
吃饭了吗16 分钟前
elementplus组件文本框设置前缀
前端·vue.js·elementui
stoneSkySpace26 分钟前
pnpm 和 npm 差异
前端·npm·node.js
欧阳码农30 分钟前
我的AI自学路线,可能对你有用
前端·人工智能·后端
掘金安东尼32 分钟前
Next.js 原生实现 PWA 离线能力
前端·javascript·next.js
前端小巷子32 分钟前
从 Vue 2 到 Vue 3
前端·vue.js·面试
全宝39 分钟前
🚀前端必学!告别样式冲突:Shadow DOM 终极指南
前端·javascript·html
GDAL40 分钟前
v-model 入门教程
前端·javascript·vue.js
excel1 小时前
前端进阶必看:你真的懂 DOM 吗?(超全总结)
前端
CF14年老兵1 小时前
Python变量与内存:每个新手都需要的灵魂拷问
前端·python·trae
excel1 小时前
你可能忽略的 DOM 扩展技巧:scrollIntoView、data-*、innerText 到性能优化
前端