npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@betomorrow/styled-tagged-text

v3.0.0

Published

Parse your text and split it into multiple components with custom styles

Downloads

1,756

Readme

Styled Tagged Text

Text component for React and React-Native application. Apply custom styles on specifics parts of your text, by enclosing them with bracket tags. Typically useful when using internationalization.

Features

  • 🧘 Easy to use: Define a StyleSheet and the style will be applied to all text enclosed with tags, using keys as tag name.
  • 👩‍💻 Web and 📱 Mobile: Use everywhere. You can use the same logic to apply rich text in your website and in your React-Native mobile app.
  • 🛠 TypeScript support: styled-tagged-text is written entirely in TypeScript
  • 🐥 Lightweight: Small library with no dependency resulting in a 5kb bundle

Usage

Installation

npm install @betomorrow/styled-tagged-text

React-native component

// style
const tagsStyle = StyleSheet.create({
	b: { fontWeight: "700" },
	i: { fontStyle: "italic" },
	emph: { color: "#eb4034", fontSize: 16 },
});

// Component
import { StyledTaggedText } from "@betomorrow/styled-tagged-text";

<StyledTaggedText tagsStyle={tagsStyle}>
	Normal [b]Bold[/b] [i][b]Bold Italic[/b] Italic[/i] [emph]Emphasize[/emph]
</StyledTaggedText>;

Render:

Render

Use <StyledTaggedText> exactly like a <Text> components.

Properties

  • tagsStyle : A record of style. Keys correspond to the tag name.
  • All Text (span on web) properties.

React component

// Define your style
const tagsStyle = {
	b: { fontWeight: "bold" },
	i: { fontStyle: "italic" },
	emph: { color: "#eb4034", fontSize: 16 },
};

// Component
import { StyledTaggedSpan } from "@betomorrow/styled-tagged-text";

<StyledTaggedSpan tagsStyle={tagsStyle}>
	Normal [b]Bold[/b] [i][b]Bold Italic[/b] Italic[/i] [emph]Emphasize[/emph]
</StyledTaggedSpan>;

Use <StyledTaggedSpan> exactly like a <span> components.

How it works ?

Underneath, StyledTaggedText (StyledTaggedSpan on web) are just nested Text (span) components with inline style. They have the exact same behavior, so you can transmit props as well.

<StyledTaggedSpan
	tagsStyle={{ b: { fontWeight: "bold" }, i: { fontStyle: "italic" }, emph: { color: "#eb4034", fontSize: 16 } }}>
	Normal [b]Bold[/b] [i][b]Bold Italic[/b] Italic[/i] [emph]Emphasize[/emph]
</StyledTaggedSpan>

Will render exactly:

<span>
	Normal
	<span style="font-weight: bold;">Bold</span>
	<span style="font-style: italic;">
		<span style="font-weight: bold;">Bold Italic</span>
		Italic
	</span>
	<span style="color: rgb(235, 64, 52); font-size: 16px;">Emphasize</span>
</span>

Example Playground

CodeBox Playground

React-Native and Web demo are available in example folder

Tips

Define a global style and wrap our component to easily re-use the same stylesheet and tags.

import { StyledTaggedText } from "@betomorrow/styled-tagged-text";
import React from "react";
import { StyleSheet, TextProps } from "react-native";

interface StyledTextProps extends TextProps {
	children?: string;
}

export const StyledText = (props: StyledTextProps) => {
	const { children, ...otherProps } = props;
	return (
		<StyledTaggedText tagsStyle={tagsStyle} {...otherProps}>
			{children}
		</StyledTaggedText>
	);
};

const tagsStyle = StyleSheet.create({
	emph: {
		color: "#1b66e4",
	},
});

And everywhere else in your app:

<StyledText>Super [emph]cool ![/emph]</StyledText>