Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div id="App" />
              
            
!

CSS

              
                .long-text {
  margin-bottom: 10px;
}

.clamp {
  -webkit-line-clamp: 5;
  -webkit-box-orient: vertical;
  display: -webkit-box;
  overflow: hidden;
  text-overflow: ellipsis;
  overflow-wrap: break-word;
}

              
            
!

JS

              
                import { LoremIpsum } from "https://cdn.skypack.dev/lorem-ipsum@2.0.3";
import classnames from "https://cdn.skypack.dev/classnames@2.3.1";
import * as lodash from "https://cdn.skypack.dev/lodash@4.17.21";

const getText = (paragraphs) => {
  const lorem = new LoremIpsum();
  return lorem.generateParagraphs(paragraphs);
};

const ReadMoreText = ({ text }) => {
  const [clamped, setClamped] = React.useState(true);
  const [showButton, setShowButton] = React.useState(true);
  const containerRef = React.useRef(null);
  const handleClick = () => setClamped(!clamped);

  React.useEffect(() => {
    const hasClamping = (el) => {
      const { clientHeight, scrollHeight } = el;
      return clientHeight !== scrollHeight;
    };

    const checkButtonAvailability = () => {
      if (containerRef.current) {
        // Save current state to reapply later if necessary.
        const hadClampClass = containerRef.current.classList.contains("clamp");
        // Make sure that CSS clamping is applied if aplicable.
        if (!hadClampClass) containerRef.current.classList.add("clamp");
        // Check for clamping and show or hide button accordingly.
        setShowButton(hasClamping(containerRef.current));
        // Sync clamping with local state.
        if (!hadClampClass) containerRef.current.classList.remove("clamp");
      }
    };

    const debouncedCheck = lodash.debounce(checkButtonAvailability, 50);

    checkButtonAvailability();
    window.addEventListener("resize", debouncedCheck);

    return () => {
      window.removeEventListener("resize", debouncedCheck);
    };
  }, [containerRef]);

  return (
    <>
      <div
        ref={containerRef}
        className={classnames("long-text", clamped && "clamp")}
      >
        {text}
      </div>
      {showButton && (
        <button onClick={handleClick}>Read {clamped ? "more" : "less"}</button>
      )}
    </>
  );
};

const App = () => {
  const text = getText(2);
  return <ReadMoreText text={text} />;
};

ReactDOM.render(<App />, document.getElementById("App"));

              
            
!
999px

Console