Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix (v8 MessageBar): Add showExpandButton prop to allow manual override to explicitly show the expand button #32884

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "minor",
"comment": "fix (v8 MessageBar): Add showExpandButton prop to allow manual override to explicitly show the expand button",
"packageName": "@fluentui/react",
"email": "[email protected]",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
Expand Up @@ -106,24 +106,52 @@ const SuccessExample = () => (
</MessageBar>
emmayjiang marked this conversation as resolved.
Show resolved Hide resolved
);

const WarningExample = (p: IExampleProps) => (
<MessageBar
messageBarType={MessageBarType.warning}
isMultiline={false}
onDismiss={p.resetChoice}
dismissButtonAriaLabel="Close"
actions={
<div>
<MessageBarButton>Action</MessageBarButton>
</div>
const WarningExample = (p: IExampleProps) => {
const [showExpandButton, setShowExpandButton] = React.useState(false);

function onResize() {
const messageBarMessage = document.getElementById('warningMessageBar') as HTMLDivElement;

if (messageBarMessage) {
const temp = messageBarMessage.cloneNode(true) as any;

temp.style.position = 'fixed';
temp.style.overflow = 'visible';
temp.style.whiteSpace = 'nowrap';
temp.style.visibility = 'hidden';

(messageBarMessage.parentElement as any).appendChild(temp);

const fullWidth = temp.getBoundingClientRect().width;
const displayWidth = messageBarMessage.getBoundingClientRect().width;

setShowExpandButton(fullWidth > displayWidth);
}
>
Warning MessageBar content.
<Link href="www.bing.com" target="_blank" underline>
Visit our website.
</Link>
</MessageBar>
);
}

window.onresize = onResize;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

two small tweaks to how the resize handler is added to window:

  • this should probably be in a useEffect with a cleanup that removes it
  • instead of the global window, you should be able to get it more safely from useWindow from @fluentui/react-window-provider


return (
<MessageBar
id="warningMessageBar"
messageBarType={MessageBarType.warning}
isMultiline={false}
onDismiss={p.resetChoice}
dismissButtonAriaLabel="Close"
actions={
<div>
<MessageBarButton>Action</MessageBarButton>
</div>
}
showExpandButton={showExpandButton}
>
Warning MessageBar content.
<Link href="www.bing.com" target="_blank" underline>
Visit our website.
</Link>
</MessageBar>
);
};

const WarningExample2 = (p: IExampleProps) => (
<MessageBar
Expand Down
1 change: 1 addition & 0 deletions packages/react/etc/react.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -7026,6 +7026,7 @@ export interface IMessageBarProps extends React_2.HTMLAttributes<HTMLElement>, R
// @deprecated
overflowButtonAriaLabel?: string;
role?: 'alert' | 'status' | 'none';
showExpandButton?: boolean;
styles?: IStyleFunctionOrObject<IMessageBarStyleProps, IMessageBarStyles>;
theme?: ITheme;
truncated?: boolean;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export const MessageBarBase: React.FunctionComponent<IMessageBarProps> = React.f
delayedRender = true,
expandButtonProps,
onExpandButtonToggled = undefined,
showExpandButton,
} = props;

// Wrap 'toggleExpandSingleLine' to execute the 'onExpandButtonToggled' callback whenever the expand button toggles
Expand Down Expand Up @@ -137,7 +138,7 @@ export const MessageBarBase: React.FunctionComponent<IMessageBarProps> = React.f
</span>
</div>
{
/* singleline expand/collapse button */ !isMultiline && !actionsDiv && truncated && (
/* singleline expand/collapse button */ (showExpandButton || (!isMultiline && !actionsDiv && truncated)) && (
<div className={classNames.expandSingleLine}>
<IconButton
disabled={false}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,13 @@ export interface IMessageBarProps extends React.HTMLAttributes<HTMLElement>, Rea
* @default true
*/
delayedRender?: boolean;

/**
* An optional override to show the expand/collapse icon. It will only be shown by default for
* single-line truncated MessageBars that do not have actions.
* @defaultvalue false
*/
showExpandButton?: boolean;
}

/**
Expand Down
Loading