0

I want to make a button in a popup window as Script Lab as follows. Note that, in Script Lab, the width of the button is enough to hold the sentence in one line, even though the popup window is not very wide:

enter image description here

I almost use the same code as ScriptLab:

import { PrimaryButton } from 'office-ui-fabric-react/lib/Button';
... ...

    return (
      <div style={{ height: '100vh', display: 'flex', flexDirection: 'column'}}>
        <PrimaryButton
          style={{ margin: 'auto' }}
          text="Open link in new window"
          // tslint:disable-next-line: jsx-no-lambda
          onClick={() => {
            window.open(this.props.url);
          }}
        />
      </div>
    );

Here is my result, where the width of the button is proportionnel to the width of the popup window. As a consequence, the sentence needs to be displayed in 2 rows, which is not what I want.

enter image description here

Does anyone know how to amend the code to get the effect like Script Lab?

Edit 1: I have made a sandbox: https://codesandbox.io/s/relaxed-feather-i6jz6?file=/src/App.js

Now, the problem is, if we Open In New Window and open https://i6jz6.csb.app/ in a new browser tab several times, we may see a little adjustment of the font of the text in the button. Does anyone know how to avoid that?

SoftTimur
  • 5,630
  • 38
  • 140
  • 292

3 Answers3

1

On button width:

In order to not have the width of the button grow proportionately with the container you can enforce the width: auto on the button. This way it will only be as wide as it needs to be to contain the text. Value auto is also better than having a fixed width, because it can automatically wrap the text if the popup becomes too narrow to display the text in one line (with fixed width your button would overflow instead - which looks really bad).

On font adjustments

For the font adjustments you experience - this is a very common thing on web and it even has its own name - FOUT (Flash of Unstyled Text). It happens when you use custom fonts on the page, because these are files like any other and so they take some time to download. Browsers prefer displaying the content as early as possible (even without custom fonts loaded) to displaying the perfect content (by waiting on all resources) with some speed penalty.

  1. The only way (at least that I know) to completely avoid FOUT is to use system fonts instead of custom fonts (github does that for example).
  2. If that's not an option, you can minimize the FOUT by caching the fonts on client machines for long times. This way they will experience the flash briefly on the first visit, but not on subsequent ones.
  3. You can also try to minimize the FOUT by telling the browser to try to preload the font files that will be needed for the page (part of the reason why FOUT happens is that browser discovers the fonts very late) https://developer.mozilla.org/en-US/docs/Web/HTML/Preloading_content
ajobi
  • 2,996
  • 3
  • 13
  • 28
0

You don't want the button's text to wrap, so you'll need to change the font size, which you can do when you find that the button's height increases when the text wraps. I suggest that you create a invisible, but not 'display: none', possibly 'off-screen' version of the button so that the real button's font is changed after you know the right size is needed. Alternatively, what about an image or glyph instead of text, and a Title for the button text?

0

Set a fixed width to the button.Setting a fixed width will make it unproportional to the width of the pop-up window. width:280px;

Second Option: If you use min-width, the button width will decrease to a point.

Third Option: If you use max-width, the button width will increase upto a point.

Fourth Option: You can also use '@media' queries to customize the width according to size of the screen.

Rishab Tyagi
  • 866
  • 1
  • 6
  • 12
  • Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackoverflow.com/rooms/215715/discussion-on-answer-by-rishab-tyagi-do-not-make-the-width-of-the-button-proport). – Samuel Liew Jun 11 '20 at 03:33