fluentui icon indicating copy to clipboard operation
fluentui copied to clipboard

Convert ContextualMenuItem to css-in-js

Open essar123 opened this issue 8 years ago • 17 comments

Bug Report Describe the issue: A few issues with the custom styles for IContextualMenuItem

IContextualMenuItem accepts a style: React.CSSProperties but its seems like its never used in code, also the method provided for customStyling of IContextualMenuItem

getItemClassNames?: (theme: ITheme,
    disabled: boolean,
    expanded: boolean,
    checked: boolean,
    isAnchorLink: boolean,
    knownIcon: boolean,
    itemClassName?: string,
    dividerClassName?: string,
    iconClassName?: string,
    subMenuClassName?: string) => IMenuItemClassNames;

is not supportive either, to implement this method in consuming code your code needs to know lot about office-ui-fabric-code.

Problems with implementing getItemClassNames in consuming repo.

  1. IMenuItemClassNames type is required to implement this method which is non importable from package.
  2. To implement this method you need to copy paste getContextualMenuClassNames in ContextualMenu.classNames.ts and make changes to desirable properties.

Suggestion: style property should be of type IContextualMenuStyles which should be merged with default styles.

essar123 avatar Mar 03 '18 12:03 essar123

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions to Fabric React!

stale[bot] avatar Apr 11 '18 01:04 stale[bot]

stay alive

oengusmacinog-zz avatar May 07 '18 17:05 oengusmacinog-zz

Any update on this?

d-khoury avatar Jun 21 '18 22:06 d-khoury

@d-khoury Currently in progress actually so there should be a PR in the next week if all goes smoothly.

oengusmacinog-zz avatar Jun 21 '18 23:06 oengusmacinog-zz

@oengusmacinog Thank you, can you update this thread when the PR is completed?

d-khoury avatar Jun 27 '18 18:06 d-khoury

@d-khoury absolutely, the PR will autoclose this issues actually

oengusmacinog-zz avatar Jun 27 '18 18:06 oengusmacinog-zz

@oengusmacinog Is there a way I can get automatically notified when the issue is resolved?

d-khoury avatar Jul 12 '18 19:07 d-khoury

@d-khoury it looks like @oengusmacinog didn't actually fix this; the PR #5461 has an explicit comment saying

Note only ContextualMenu has been converted, not ContextualMenuItem due to time constraints.

@oengusmacinog what is your plan for finishing this? @cliffkoh had a very astute comment in the PR...

Should we really check in stuff halfway? It makes it really confusing as to the state and usability of the API (personally, I don't even check it in in 2 parts because of this problem).

metapzl avatar Aug 02 '18 16:08 metapzl

I won't be able to do the ContextualMenuItem. @micahgodbolt do you know who might be able to pick this up?

oengusmacinog-zz avatar Aug 02 '18 18:08 oengusmacinog-zz

I can make sure it gets done. I don't know if I'll have the time to dive into it right now.

micahgodbolt avatar Aug 02 '18 18:08 micahgodbolt

Setting as pri 1 because the component is in a halfway state atm, which is bad :(

cliffkoh avatar Aug 02 '18 19:08 cliffkoh

@KevinTCoughlin said he could dive into this. There's several questions about implementation though. As a child we could use subComponentStyles, or the contextualMenuItemAs, OR pass a styles prop through each item.....and the solution is probably a combination of the 3

micahgodbolt avatar Aug 02 '18 19:08 micahgodbolt

I was going to take a look at TextField and any other components that support a similar rendering paradigm and figure it out. Pointers welcome.. been some time since I looked at this, but I think I can figure it out.

KevinTCoughlin avatar Aug 02 '18 20:08 KevinTCoughlin

scenarios to support: Single styles function to style entire contextualMenu Ability to apply custom styles per item Ability to style differently based on what type of item it is (header, divider, regular etc)

micahgodbolt avatar Aug 03 '18 16:08 micahgodbolt

Work

  • [x] Get working example w/ ContextualMenuItem styled via item props (styles, style, className, classNames)
  • [x] Get working example w/ ContextualMenu styled via props (styles, getMenuClassNames)
  • [x] Apply deprecation warnings where necessary
  • [x] Verify public API same i.e. no breaking changes

Test Scenarios

  • [x] Single styles function to style entire contextualMenu
  • [x] Ability to apply custom styles per item
  • [x] Ability to style differently based on what type of item it is (header, divider, regular etc)

KevinTCoughlin avatar Aug 06 '18 18:08 KevinTCoughlin

I've raised a PR, but am going thru the various modes of styling once more to make sure we're covered prior to merge. Getting close..

KevinTCoughlin avatar Aug 10 '18 00:08 KevinTCoughlin

Thanks for your patience and for the initial report @essar123. I've converted ContextualMenu to use getStyles for itself as well as the ContextualMenuItem(s). I've also deprecated the classNames approach and related fields / types.

This comment goes over the various ways to use getStyles https://github.com/OfficeDev/office-ui-fabric-react/pull/6094#issuecomment-417423698.

For styling various aspects of the menu, you can provide a styles? prop as part of menuProps:

const getMenuStyles = (): Partial<IContextualMenuStyles> => {
  return {
    root: {
      background: 'blue'
    }
  };
};

...

menuProps={{
  shouldFocusOnMount: true,
  styles: getMenuStyles,

...

For styling individual items within a menu, you can provide a styles? within itemProps? per IContextualMenuItem definition:

const getMenuItemStyles = (): Partial<IContextualMenuItemStyles> => {
  return {
    linkContent: {
      color: 'red'
    }
  };
};

...

menuProps={{
  shouldFocusOnMount: true,
  items: [
    {
      key: 'newItem',
      text: 'New',
      onClick: () => console.log('New clicked'),
      itemProps: { styles: getMenuItemStyles }
    },

...

More information about the styles prop can be find in our Wiki

KevinTCoughlin avatar Sep 05 '18 00:09 KevinTCoughlin