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

react-clamped

v1.0.1

Published

A React Library to handle multiline ellipsises and to clamp the text to a fixed line number.

Downloads

699

Readme

React Clamped

A React Library to handle multiline ellipsises and to clamp the text to a fixed line number.

React-Clamped is a lightweight clamping library which allows you to create multiline ellipsis texts in react apps. Its bundle size is only 724B. Since it is very lightweight, you can use it any project and it will have almost no impact ont bundle size of the app.

Find the demo in this link: https://xenoverseup.github.io/react-clamped/

NPM JavaScript Style Guide

Install

npm install --save react-clamped

Usage

Basic Usage

Just import the Clamped component and wrap your content inside. The line number can be adjusted with clamp prop. The default for clamp prop is "1".

import React, { Component } from 'react'

import { Clamped } from 'react-clamped'

class Card extends Component {
  render() {
    return (
      <div className="card">
        <Clamped clamp={4}>{...your multiline content...}</Clamped>
      </div>
    )
  }
}

useClamped Hook

You can truncate the lines to a fixed number with useClamped hook, as well. Import the hook and initialize. Then assign the ref to the element you want to truncate.

import React, { Component } from 'react'

import { useClamped } from 'react-clamped'

const Card = () => {
  const ref = useClamped({
    clamp: 3,
    isClamped: true
  })

  return (
      <div className="card">
        <p ref={ref}>{...your multiline content...}</p>
      </div>
    )
}

Props

  • clamp : Number

    This prop determines how many lines should be rendered. The "clamp" can be used in both "Clamped" component as a prop and in "useClamped" hook as an object entry. Default value is 1.

  • element : String

    The "element" prop can be used in only "Clamped" component as a prop and it determines which DOM element should be used to wrap content. Default value is "p".

  • isClamped : Boolean

    This prop determines if the text should be truncated. The "isClamped" can be used in both "Clamped" component as a prop and in "useClamped" hook as an object entry. This prop can be very handy if you want to make a "show more" button. The default value if true.

Example

Here is an example of making collapsible text with React Clamped.

import React, { useState } from 'react'
import { Clamped, useClamped } from 'react-clamped'
import './App.css'

const App = () => {
  const [state, setState] = useState({
    component: true,
    hook: true
  })

  const ref = useClamped({
    clamp: 3,
    isClamped: state.hook
  })

  return (
    <div className='App'>
      <section>
        <div className='clamped-component'>
          <header>
            <h2>Am I clamped?</h2>
            <span>Clamped Component</span>
          </header>
          <Clamped clamp={3} isClamped={state.component} element='p'>
            Lorem ipsum dolor sit amet consectetur adipisicing elit. Natus eos
            temporibus voluptas porro earum sequi ipsum aut aliquam accusamus id
            saepe necessitatibus impedit nesciunt accusantium dolor culpa,
            corporis suscipit nisi molestias. Neque ipsa officia quis explicabo
            asperiores itaque porro facilis perspiciatis? Eaque, eos? Fuga
            facilis voluptate, molestiae maiores pariatur culpa excepturi nulla
            doloribus laboriosam minus mollitia porro dicta, cumque repellat
            illo, cum tempore itaque possimus repellendus corporis? Illo, quod
            consequuntur?
          </Clamped>
          <button
            onClick={() => setState({ ...state, component: !state.component })}
          >
            {state.first ? 'Show More' : 'Show Less'}
          </button>
        </div>
        <div className='use-clamped'>
          <header>
            <h2>Did you use clamp?</h2>
            <span>useClamped Hook</span>
          </header>
          <p ref={ref}>
            Lorem ipsum dolor sit amet consectetur adipisicing elit. Natus eos
            temporibus voluptas porro earum sequi ipsum aut aliquam accusamus id
            saepe necessitatibus impedit nesciunt accusantium dolor culpa,
            corporis suscipit nisi molestias. Neque ipsa officia quis explicabo
            asperiores itaque porro facilis perspiciatis? Eaque, eos? Fuga
            facilis voluptate, molestiae maiores pariatur culpa excepturi nulla
            doloribus laboriosam minus mollitia porro dicta, cumque repellat
            illo, cum tempore itaque possimus repellendus corporis? Illo, quod
            consequuntur?
          </p>
          <button onClick={() => setState({ ...state, hook: !state.hook })}>
            {state.second ? 'Show More' : 'Show Less'}
          </button>
        </div>
      </section>
    </div>
  )
}

export default App

License

MIT © XenoverseUp