Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

msteams-react-base-component

wictorwilen6.3kMIT4.0.1TypeScript support: included

React helper components for Microsoft 365 Teams, Office and Outlook apps

react, microsoft-teams, msteams, typescript, fluentui, mgt

readme

Microsoft Teams UI Controls base component

npm version npm MIT GitHub issues GitHub closed issues

This is a set of React hooks and providers based on the Microsoft Teams JavaScript SDK, the Fluent UI components and Microsoft Graph Toolkit, which is used when generating Microsoft Teams Apps using the Microsoft Teams Yeoman Generator.

@master @preview
Build Status Build Status

Usage

useTeams hook

To use this package in a Teams tab or extension import the useTeams Hook and then call it inside a functional component.

const [{inTeams}] = useTeams();

The useTeams hook will return a tuple of where an object of properties are in the first field and an object of methods in the second.

NOTE: using the hook will automatically initialize the Microsoft Teams JS SDK is available.

useTeams Hook arguments

The useTeams hook can take an optional object argument:

Argument Description
initialTheme?: string Manually set the initial theme (default, dark or contrast)
setThemeHandler?: (theme?: string) => void Custom handler for themes

Available properties

Property name Type Description
inTeams boolean? true if hosted in Teams and false for outside of Microsoft Teams
fullScreen boolean? true if the Tab is in full-screen, otherwise false
themeString string The value of default, dark or contrast
theme ThemePrepared The Fluent UI Theme object for the current theme
context Context? undefined while the Tab is loading or if not hosted in Teams, set to a value once the Tab is initialized and context available

Available methods

Method name Description
setTheme(theme?: string) Method for manually setting the theme

Full example

Example of usage:

import * as React from "react";
import { Provider, Flex, Header } from "@fluentui/react-northstar";
import { useState, useEffect } from "react";
import { useTeams } from "msteams-react-base-component";

/**
 * Implementation of the hooks Tab content page
 */
export const HooksTab = () => {
    const [{ inTeams, theme }] = useTeams({});
    const [message, setMessage] = useState("Loading...");

    useEffect(() => {
        if (inTeams === true) {
            setMessage("In Microsoft Teams!");
        } else {
            if (inTeams !== undefined) {
                setMessage("Not in Microsoft Teams");
            }
        }
    }, [inTeams]);

    return (
        <Provider theme={theme}>
            <Flex fill={true}>
                <Flex.Item>
                    <Header content={message} />
                </Flex.Item>
            </Flex>
        </Provider>
    );
};

License

Copyright (c) Wictor Wilén. All rights reserved.

Licensed under the MIT license.

changelog

Change Log

All notable changes to this project will be documented in this file.

The format is based on Keep a Changelog and this project adheres to Semantic Versioning.

[4.0.1] - <2022-06-28>

Fixes

  • Fixed an issue when the useTeams hook did not get the context properly

[4.0.0] - <2022-06-23>

Changes

  • Removed support for Node 10.x
  • Node 12.x still available but not tested and pending removal
  • Upgraded @microsoft/teams-js SDK to version 2

Deleted

  • Deleted the checkInTeams helper method
  • Deleted the getQueryVariable helper method

[3.1.1] - <2021-09-30>

Changes

  • Dependency bump

Fixes

  • Fixed an issue where non source files was included in tests
  • Fixed an issue to use userAgent to detect Teams desktop client (#20)

[3.1.0] - <2021-03-01>

Added

  • Added setThemeHandler option (#13)
  • Added unit testing

Fixes

  • Fixed an issue with the @fluentui/react-northstar peer dependency version range with npm 7+

[3.0.2] - <2021-01-29>

Fixes

  • Fixed issue where the Teams JS Library was not initialized while hosted outside of Teams (for instance in a popup)

[3.0.1] - <2020-12-18>

Changes

  • inTeams is undefined by default, instead of false
  • Optimized the hook to reduce the number of renders of the component using the hook

[3.0.0] - <2020-12-06>

Added

  • Added useTeams React hook
  • Exported helper method getQueryVariable
  • Exported helper method checkInTeams

Changes

  • Method detecting if the hook is used inside Microsoft Teams now relies on naming of the hosting iframe
  • Moved to Github actions from Travis CI

Removed

  • Base class removed

[2.2.0] - <2020-10-27>

Added

  • Added esm module version of the package (#5)

Changed

  • Updated @microsoft/teams-js to version ^1.8.0
  • Updated @fluentui/react-northstar to version ^0.51.0.

[2.1.0] - <2020-05-17>

Changed

  • Breaking change, inTeams(timeout:number=1000) now returns a Promise<Boolean>
  • Switched to eslint and removed tslint
  • Switched to renamed package @fluentui/react-northstar

Deleted

  • Removed unnecessary constructor
  • Removed fontSize from state
  • Removed pageFontSize protected method
  • Removed the property interface
  • Removed the setValidityState method

[2.0.0] - <2020-03-05>

Changed

  • Replaced msteams-ui-components-react with @fluentui/react
  • Updated dependecies (@microsoft/teams-js)
  • Refactored packages into peerDependencies

[1.1.1] - <2019-04-17>

Fixes

  • Fixed an issue with React typings version

[1.1.0] - <2019-03-24>

Changed

  • Updated Typescript, React, teams-js and msteams-ui-components-react libraries

[1.0.0] - <2018-08-13>

Changed

  • Updated version of @microsoft/teams-js

[0.0.4] - <2018-07-30>

Changed

  • Updated code to reflect linting changes

Fixed

  • Fixed missing import to the @microsoft/teams-js library

Added

  • Travis-ci integration
  • Linting added (npm run lint)

[0.0.3] - <2018-07-28>

Changed

  • Changed signature for getQueryVariable and updateTheme

[0.0.2] - <2018-07-28>

Added

  • More inline documentation

Changed

  • Included comments in output
  • Updated node packages
  • Updated method signatures
  • Updated tsconfig.json with stricter checking

[0.0.1] - <2018-07-28>

Added

  • Initial release