0

I am working on the MS Teams app where I have a page in which I want to identify current MS Teams theme (default, dark or high contrast).

I have added this https://www.npmjs.com/package/msteams-react-base-component dependency where it explaining how we can extract teams theme. Below is my code snippet which always giving me themeString as default.

export default function FioriTab(): ReactElement {
    const [{ themeString }] = useTeams({});

    useEffect(() => {
        console.log(themeString)
    }, [themeString]);
}

Any idea what I am doing wrong here.

I tried adding setThemeHandler which also returning theme as undefined.

enter image description here

Shabbir Dhangot
  • 8,954
  • 10
  • 58
  • 80

1 Answers1

0

With respect to Wictor, I don't know much about that library you're referencing, but according to the docs on this one (https://github.com/OfficeDev/TeamsFx), it's based on his anyway and probably better maintained, considering. It also has a useTeams hook that aims to do the same thing.

If you have a totally new solution anyway, perhaps best is to start with the Teams Toolkit in any case, as it will scaffold this for you.

On a separate but related note, see that useTeams also allows you to send a theme handler - this is important to handle theme changes while a user is using your apps - Teams will let you know the theme has changed so you can respond accordingly.

With regards your main question, the reason you're always seeing the "default" theme, from what I can see in the code for this framework, is because of the asynchronous setting of the theme state over here. It can be easily solved, per my suggestion above, of implementing the theme change event handler that you need to implement anyway. Basically, you should have something like this:

export default function FioriTab(): ReactElement {
  const [themeString, setThemeString] = useState<string>("default");

  const [{ inTeams }] = useTeams({}, (theme: string | undefined) => {
    setThemeString(theme || "default");
  });

...
Hilton Giesenow
  • 9,809
  • 2
  • 10
  • 24
  • I worked and removed this lib and consumed official lib. Its giving same result what above API is giving. – Shabbir Dhangot Nov 17 '22 at 15:54
  • I am creating tab with website type and want to change theme of the website according to the teams. I am able to see themeString value working on the other component but only website component showing themeString as default all the time. – Shabbir Dhangot Nov 17 '22 at 16:17
  • When you're debugging, does your callback (line 22) get called twice? From what I can see, it would get called once with `undefined` and then again with the *actual* theme. See https://github.com/OfficeDev/TeamsFx/blob/f43656338f2cfe501192be7d7dc08a163e46df94/packages/sdk-react/src/useTeams.ts#L78 and then again on https://github.com/OfficeDev/TeamsFx/blob/f43656338f2cfe501192be7d7dc08a163e46df94/packages/sdk-react/src/useTeams.ts#L91 – Hilton Giesenow Nov 17 '22 at 17:38
  • basically, it's that *2nd* call that should set the theme properly. – Hilton Giesenow Nov 17 '22 at 17:38
  • app.initialize itself is failing. Am I doing anything wrong? – Shabbir Dhangot Nov 17 '22 at 18:30
  • What does 'failing' mean? – Hilton Giesenow Nov 18 '22 at 07:22
  • Is this project based on Teams Toolkit? If not, how did you start the project? Maybe something is missing. In these cases, sometimes it's better to start from a fresh template – Hilton Giesenow Nov 18 '22 at 07:22
  • Yes project is based on the Teams Toolkit. I am receiving theme as dark in other component. Only one specific component is not receiving theme properly. – Shabbir Dhangot Nov 18 '22 at 11:40
  • All components are in the same solution? – Hilton Giesenow Nov 18 '22 at 12:27
  • yes it's strange only one component is not receiving theme. Actually that component loads when in one to one chat website is share. While loading website it picks route and loading app.tsx and specific component where I want to apply theme according to the MS Teams theme. But at there component never receiving dark/contrast theme, and receiving only default theme. – Shabbir Dhangot Nov 19 '22 at 05:25
  • I can't think then of any reason why that one component would not be working. Sorry – Hilton Giesenow Nov 19 '22 at 16:52
  • Thank you so much for help Hilton, do you think it's bug on MS side and any specific repo where we can raise this? – Shabbir Dhangot Nov 21 '22 at 08:08
  • No I doubt it, I've never seen it happen. It's probably something in the chain of the application itself, but I'd need to see the whole source code to see and I'm sure you wouldn't want to share it on this site. There are a bunch of Microsoft staff who watch here though, maybe let's see if they can help – Hilton Giesenow Nov 21 '22 at 08:11
  • I have tried everything now, I have created github issue where this can be reproduce. https://github.com/OfficeDev/TeamsFx/issues/7050 see if you can also reproduce issue at your end – Shabbir Dhangot Dec 01 '22 at 10:40