-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
[core/public/banners] migrate ui/notify/banners #23215
Closed
spalger
wants to merge
6
commits into
elastic:master
from
spalger:migrate-new-platform/global-banners
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4965576
[core/public/banners] migrate ui/notify/banners
39799e1
Merge branch 'master' of github.com:elastic/kibana into migrate-new-p…
fc52d56
[core/public/banners] refactor to not expose react in contract, impro…
d90a6e7
[core/public/banners] remove react/component verbiage from docs
ba227e2
[core/public/banners] remove old import
16cdfff
exclude banners from i18n until #23268 is resolved
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
190 changes: 190 additions & 0 deletions
190
src/core/public/notifications/banners/banners_service.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,190 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import { unmountComponentAtNode } from 'react-dom'; | ||
import { BannersService } from './banners_service'; | ||
|
||
function renderFn(innerHTML: string) { | ||
return (el: HTMLDivElement) => { | ||
el.innerHTML = innerHTML; | ||
return () => (el.innerHTML = ''); | ||
}; | ||
} | ||
|
||
describe('start.add()', () => { | ||
it('renders the component in the targetDomElement', () => { | ||
const targetDomElement = document.createElement('div'); | ||
const start = new BannersService({ targetDomElement }).start(); | ||
start.add(renderFn('foo')); | ||
|
||
expect(targetDomElement).toMatchInlineSnapshot(` | ||
<div> | ||
<div | ||
class="globalBanner__list" | ||
> | ||
<div | ||
class="globalBanner__item" | ||
> | ||
foo | ||
</div> | ||
</div> | ||
</div> | ||
`); | ||
}); | ||
|
||
it('renders higher-priority banners abover lower-priority ones', () => { | ||
const targetDomElement = document.createElement('div'); | ||
const start = new BannersService({ targetDomElement }).start(); | ||
start.add(renderFn('100'), 100); | ||
start.add(renderFn('1'), 1); | ||
start.add(renderFn('200'), 200); | ||
|
||
expect(targetDomElement).toMatchInlineSnapshot(` | ||
<div> | ||
<div | ||
class="globalBanner__list" | ||
> | ||
<div | ||
class="globalBanner__item" | ||
> | ||
200 | ||
</div> | ||
<div | ||
class="globalBanner__item" | ||
> | ||
100 | ||
</div> | ||
<div | ||
class="globalBanner__item" | ||
> | ||
1 | ||
</div> | ||
</div> | ||
</div> | ||
`); | ||
}); | ||
}); | ||
|
||
describe('start.remove()', () => { | ||
it('removes the component from the targetDomElement', () => { | ||
const targetDomElement = document.createElement('div'); | ||
const start = new BannersService({ targetDomElement }).start(); | ||
const id = start.add(renderFn('foo')); | ||
start.remove(id); | ||
expect(targetDomElement).toMatchInlineSnapshot(`<div />`); | ||
}); | ||
|
||
it('does nothing if the id is unknown', () => { | ||
const targetDomElement = document.createElement('div'); | ||
const start = new BannersService({ targetDomElement }).start(); | ||
start.add(renderFn('foo')); | ||
start.remove('something random'); | ||
expect(targetDomElement).toMatchInlineSnapshot(` | ||
<div> | ||
<div | ||
class="globalBanner__list" | ||
> | ||
<div | ||
class="globalBanner__item" | ||
> | ||
foo | ||
</div> | ||
</div> | ||
</div> | ||
`); | ||
}); | ||
}); | ||
|
||
describe('start.replace()', () => { | ||
it('replaces the banner with the matching id', () => { | ||
const targetDomElement = document.createElement('div'); | ||
const start = new BannersService({ targetDomElement }).start(); | ||
const id = start.add(renderFn('foo')); | ||
expect(targetDomElement).toMatchInlineSnapshot(` | ||
<div> | ||
<div | ||
class="globalBanner__list" | ||
> | ||
<div | ||
class="globalBanner__item" | ||
> | ||
foo | ||
</div> | ||
</div> | ||
</div> | ||
`); | ||
start.replace(id, renderFn('bar')); | ||
expect(targetDomElement).toMatchInlineSnapshot(` | ||
<div> | ||
<div | ||
class="globalBanner__list" | ||
> | ||
<div | ||
class="globalBanner__item" | ||
> | ||
bar | ||
</div> | ||
</div> | ||
</div> | ||
`); | ||
}); | ||
|
||
it('adds the banner if the id is unknown', () => { | ||
const targetDomElement = document.createElement('div'); | ||
const start = new BannersService({ targetDomElement }).start(); | ||
start.add(renderFn('foo')); | ||
start.replace('something random', renderFn('bar')); | ||
expect(targetDomElement).toMatchInlineSnapshot(` | ||
<div> | ||
<div | ||
class="globalBanner__list" | ||
> | ||
<div | ||
class="globalBanner__item" | ||
> | ||
foo | ||
</div> | ||
<div | ||
class="globalBanner__item" | ||
> | ||
bar | ||
</div> | ||
</div> | ||
</div> | ||
`); | ||
}); | ||
}); | ||
|
||
describe('stop', () => { | ||
it('unmounts the component from the targetDomElement', () => { | ||
const targetDomElement = document.createElement('div'); | ||
const service = new BannersService({ targetDomElement }); | ||
service.start(); | ||
service.stop(); | ||
expect(unmountComponentAtNode(targetDomElement)).toBe(false); | ||
}); | ||
|
||
it('cleans out the content of the targetDomElement', () => { | ||
const targetDomElement = document.createElement('div'); | ||
const service = new BannersService({ targetDomElement }); | ||
service.start().add(renderFn('foo-bar')); | ||
service.stop(); | ||
expect(targetDomElement).toMatchInlineSnapshot(`<div />`); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import React from 'react'; | ||
import { render, unmountComponentAtNode } from 'react-dom'; | ||
import * as Rx from 'rxjs'; | ||
|
||
import { GlobalBannersContainer } from './containers/global_banners_container'; | ||
|
||
export interface Banner { | ||
readonly id: string; | ||
readonly priority: number; | ||
readonly render: (targetDomElement: HTMLDivElement) => (() => void) | void; | ||
} | ||
|
||
export type Banners = Banner[]; | ||
|
||
interface Params { | ||
targetDomElement: HTMLElement; | ||
} | ||
|
||
export class BannersService { | ||
constructor(private readonly params: Params) {} | ||
|
||
public start() { | ||
let uniqueId = 0; | ||
const banners$ = new Rx.BehaviorSubject<Banners>([]); | ||
|
||
render( | ||
<GlobalBannersContainer banners$={banners$.asObservable()} />, | ||
this.params.targetDomElement | ||
); | ||
|
||
return { | ||
/** | ||
* Add a banner that should be rendered at the top of the page along with an optional priority. | ||
*/ | ||
add: (renderFn: Banner['render'], priority = 0) => { | ||
const id = `${++uniqueId}`; | ||
banners$.next([...banners$.getValue(), { id, priority, render: renderFn }]); | ||
return id; | ||
}, | ||
|
||
/** | ||
* Remove a banner from the top of the page. | ||
*/ | ||
remove: (id: string) => { | ||
banners$.next(banners$.getValue().filter(banner => banner.id !== id)); | ||
}, | ||
|
||
/** | ||
* Replace a banner and its priority. If the render function is not === to the | ||
* previous render function the previous banner will be unmounted and re-rendered. | ||
*/ | ||
replace: (id: string, renderFn: Banner['render'], priority = 0) => { | ||
const newId = `${++uniqueId}`; | ||
banners$.next([ | ||
...banners$.getValue().filter(banner => banner.id !== id), | ||
{ id: newId, priority, render: renderFn }, | ||
]); | ||
return newId; | ||
}, | ||
}; | ||
} | ||
|
||
public stop() { | ||
unmountComponentAtNode(this.params.targetDomElement); | ||
this.params.targetDomElement.textContent = ''; | ||
} | ||
} | ||
|
||
export type BannersStartContract = ReturnType<BannersService['start']>; |
4 changes: 0 additions & 4 deletions
4
...ic/notify/banners/global_banner_list.less → ...banners/components/global_banner_item.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,3 @@ | ||
.globalBanner__list { | ||
padding: 16px; | ||
} | ||
|
||
.globalBanner__item + .globalBanner__item { | ||
margin-top: 16px; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
note: wondering how bad is just
.globalBanner__item { margin-botton: 16px; }
so that we resort to+
...