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-docx

v1.0.1

Published

React Reconciler for DOCX - build Docx with JSX

Downloads

833

Readme

react-docx https://www.npmjs.com/package/react-docx

react-docx is a React reconciler for DOCX.js.

Why

Largely inspired by R3F this library allows you write your DOCX documents in declarative style with reusable components. This is not a wrapper library and so DOCX classes are trasformed dynamicly into React components.

Limitations

In current early stage of library and because of inconsistent style of some methods in DOCX.js not all advanced DOCX features may work declaratively. Currently only single render run is supported, that means that using useState or passing another set of values to Context providers will have no effect. Alternatively you can call renderAsyncDocument again to rerender the document entirely.

Usage

Instal peer dependencies React and Docx! They are needed for JSX and DOCX elements.

import React from "react"; // that is needed for jsx to work
import { renderAsyncDocument } from "react-docx";
import * as Docx from "docx"; // that is a peer dependency

renderAsyncDocument(
  <section>
    <paragraph heading={Docx.HeadingLevel.HEADING_1}>
      You can pass props as if you are passing them to constructor
    </paragraph>
    <p>There are some helpful shortcuts for often used tags, like this</p>
    <p>
      <t>this one is for TextRun</t>
    </p>
    <p>
      For text inside anything else than t or textrun tags, TextRun is created
      under the hood
      <t break>
        For when docx objects have functional methods, you can pass function
        name as a prop. Prop value will be passed as an argument, if you need to
        pass many arguments use array instead.
      </t>
    </p>
    <p>
      Docx object is considered a tag if it has a constructor signature and is
      exported out of DOCX.js. Object key is then converted to lowercase.
      Everything you can make with raw DOCX.js(including mistakes), you can make
      here.
    </p>
    <p>
      <img
        src="base64 string or buffer object works"
        width={200}
        height={200}
      />
      <href
        src="http://localhost:8080"
        label={"For images and links shortcuts object are provided"}
      />
      This allows for removal of boilerplate for often used objects. In future
      more such object will be implemented.
    </p>
    <p>
      Sections are root tags for valid DOCX. If you want to have multiple
      sections, pass React Fragment with them to renderAsyncDocument.
    </p>
    <Component text="You can use componets of course, just like in react!">
      <t>A child</t>
    </Component>
  </section>
).then((document) => console.log("This is rendered docx document", document));

const Component = ({ children, text }) => {
  const ref = React.useRef(null); // use can use refs to access docx objects
  React.useLayoutEffect(() => {
    // you can use LayoutEffect hook in combination with refs to write imperative code( hacks for example)
    console.log(ref.current);
    // because of useLayoutEffect nature it will work in single render as opposed to regular useEffect
  }, []);
  return (
    <p ref={ref}>
      <t>{text}</t>
      {children}
    </p>
  );
};

API

For details on Docx refer to it's docs.

Core render function, returns promise that is resolved with rendered document object. You can use Docx tools to convert it to blob, as according to Docx examples.

renderAsyncDocument(ReactElements, DocumentProperites, FileProperties);

Future Plans

  • More shortcuts
  • More fictive objects to remove boilerplate
  • Add mutations to Document tree