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 settings menu title cut off by scrollbar #599

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/lib/viewers/media/Settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,11 +292,15 @@ class Settings extends EventEmitter {
* @return {void}
*/
setMenuContainerDimensions(menu) {
// NOTE: need to explicitly set the dimensions in order to get css transitions. width=auto doesn't work with css transitions
this.settingsEl.style.width = `${menu.offsetWidth + 18}px`;
const { children } = menu;

// If we have enough children to require scrolling, take into account scroll bar width.
const scrollPadding = children.length > 7 ? 32 : 18;
Copy link
Contributor

Choose a reason for hiding this comment

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

Extract these magic numbers to constants

Copy link
Contributor

@jeremypress jeremypress Jan 23, 2018

Choose a reason for hiding this comment

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

Would looking at scrollHeight help?

The Element.scrollHeight read-only property is a measurement of the height of an element's content, including content not visible on the screen due to overflow.

https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollHeight

this.settingsEl.style.width = `${menu.offsetWidth + scrollPadding}px`;

// height = n * $item-height + 2 * $padding (see Settings.scss) + 2 * border (see Settings.scss)
// where n is the number of displayed items in the menu
const sumHeight = [].reduce.call(menu.children, (sum, child) => sum + child.offsetHeight, 0);
const sumHeight = [].reduce.call(children, (sum, child) => sum + child.offsetHeight, 0);
Copy link
Contributor

Choose a reason for hiding this comment

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

I was taking a look at this before and was trying to figure out why it iterates over the children and sums up the offsetHeight vs taking the menu.offsetHeight. Seemed to work with the menu offsetHeight. Any ideas?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not too sure, but I can confirm that menu.offsetHeight is the way to go.

this.settingsEl.style.height = `${sumHeight + 18}px`;
}

Expand Down
41 changes: 41 additions & 0 deletions src/lib/viewers/media/__tests__/Settings-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,47 @@ describe('lib/viewers/media/Settings', () => {
});
});

describe('setMenuContainerDimensions', () => {
it('should add padding to settingsEl based on menu contents and additional padding', () => {
const menuEl = document.createElement('div');
menuEl.appendChild(document.createElement('span'));
settings.setMenuContainerDimensions(menuEl);

expect(settings.settingsEl.style.width).to.equal('18px');
});

it('should add extra padding to settingsEl based on menu contents that require scroll bar', () => {
const menuEl = document.createElement('div');
// Greater than enough to add a scroll bar.
menuEl.appendChild(document.createElement('span'));
Copy link
Contributor

Choose a reason for hiding this comment

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

Add one element and set it's size with js?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This will work especially since we're no longer counting child heights, and using offsetHeight

menuEl.appendChild(document.createElement('span'));
menuEl.appendChild(document.createElement('span'));
menuEl.appendChild(document.createElement('span'));
menuEl.appendChild(document.createElement('span'));
menuEl.appendChild(document.createElement('span'));
menuEl.appendChild(document.createElement('span'));
menuEl.appendChild(document.createElement('span'));
settings.setMenuContainerDimensions(menuEl);

expect(settings.settingsEl.style.width).to.equal('32px');
});

it('should grow the height of the settingsEl based on the number of child elements inside of it', () => {
const menuEl = {
offsetWidth: 0,
children: [
{
offsetHeight: 18
}
]
};
settings.setMenuContainerDimensions(menuEl);

// Adds 18px to the offsetHeight of sum of child element's heights
expect(settings.settingsEl.style.height).to.equal('36px');
});
});

describe('destroy()', () => {
it('should remove event listeners on settings element and document', () => {
sandbox.stub(settings.settingsEl, 'removeEventListener');
Expand Down