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

@microsoft/mgt-react

v4.4.0

Published

Microsoft Graph Toolkit React wrapper class

Downloads

51,792

Readme

React wrapper for Microsoft Graph Toolkit

npm

Use mgt-react to simplify usage of Microsoft Graph Toolkit (mgt) web components in React. The library wraps all mgt components and exports them as React components.

Installation

npm install @microsoft/mgt-react

or

yarn add @microsoft/mgt-react

Usage

All components are available via the npm package and are named using PascalCase. To use a component, first import it at the top:

import { Person } from '@microsoft/mgt-react';

You can now use Person anywhere in your JSX as a regular React component.

<Person personQuery="me" />

All properties and events map exactly as they are defined in the component documentation - see web component docs.

For example, you can set the personDetails property to an object:

const App = (props) => {
  const personDetails = {
    displayName: 'Bill Gates',
  };

  return <Person personDetails={personDetails}></Person>;
};

Or, register an event handler:

import { PeoplePicker, People } from '@microsoft/mgt-react';

const App = (props) => {
  const [people, setPeople] = useState([]);

  const handleSelectionChanged = (e) => {
    setPeople(e.target.selectedPeople);
  };

  return
    <div>
      <PeoplePicker selectionChanged={handleSelectionChanged} />
      Selected People: <People people={people} />
    </div>;
};

Templates

Most Microsoft Graph Toolkit components support templating and mgt-react allows you to leverage React for writing templates.

For example, to create a template to be used for rendering events in the mgt-agenda component, first define a component to be used for rendering an event:

import { MgtTemplateProps } from '@microsoft/mgt-react';

const MyEvent = (props: MgtTemplateProps) => {
  const { event } = props.dataContext;
  return <div>{event.subject}</div>;
};

Then use it as a child of the wrapped component and set the template prop to event

import { Agenda } from '@microsoft/mgt-react';

const App = (props) => {
  return <Agenda>
    <MyEvent template="event">
  </Agenda>
}

The template prop allows you to specify which template to overwrite. In this case, the MyEvent component will be repeated for every event, and the event object will be passed as part of the dataContext prop.

Custom hooks

mgt-react exposes some custom hooks that you can use in your app:

useIsSignedIn

You can use this hook to check the signed in state:

import { Agenda, useIsSignedIn } from '@microsoft/mgt-react';

const App = (props) => {
  const [isSignedIn] = useIsSignedIn();

  return {isSignedIn && <Agenda></Agenda>}
}

Why

If you've used web components in React, you know that proper interop between web components and React components requires a bit of extra work.

From https://custom-elements-everywhere.com/:

React passes all data to Custom Elements in the form of HTML attributes. For primitive data this is fine, but the system breaks down when passing rich data, like objects or arrays. In these instances you end up with stringified values like some-attr="[object Object]" which can't actually be used.

Because React implements its own synthetic event system, it cannot listen for DOM events coming from Custom Elements without the use of a workaround. Developers will need to reference their Custom Elements using a ref and manually attach event listeners with addEventListener. This makes working with Custom Elements cumbersome.