2018-12-08 07:23:48 +00:00
|
|
|
<div class="compose-box-button-sentinel" ref:sentinel></div>
|
2018-12-02 19:22:18 +00:00
|
|
|
<div class="compose-box-button-wrapper {showSticky ? 'compose-box-button-sticky' : ''} {hideAndFadeIn}"
|
|
|
|
ref:wrapper >
|
|
|
|
<ComposeButton {overLimit} {sticky} on:click="onClickButton()" />
|
|
|
|
</div>
|
|
|
|
<style>
|
|
|
|
.compose-box-button-wrapper {
|
|
|
|
/*
|
|
|
|
* We want pointer-events only for the sticky button, so use fit-content so that
|
|
|
|
* the element doesn't take up the full width, and then set its left margin to
|
|
|
|
* auto so that it sticks to the right. fit-content doesn't work in Edge, but
|
|
|
|
* that just means that content that is level with the button is not clickable.
|
|
|
|
*/
|
|
|
|
width: -moz-fit-content;
|
|
|
|
width: fit-content;
|
|
|
|
margin-left: auto;
|
|
|
|
display: flex;
|
|
|
|
justify-content: flex-end;
|
|
|
|
}
|
|
|
|
|
|
|
|
.compose-box-button-sticky {
|
|
|
|
position: -webkit-sticky;
|
|
|
|
position: sticky;
|
|
|
|
}
|
|
|
|
|
|
|
|
:global(.compose-box-button-sticky, .compose-box-button-fixed) {
|
|
|
|
z-index: 5000;
|
|
|
|
top: 52px; /* padding-top for .main-content plus 10px */
|
|
|
|
}
|
|
|
|
|
|
|
|
@media (max-width: 767px) {
|
|
|
|
:global(.compose-box-button-sticky, .compose-box-button-fixed) {
|
|
|
|
top: 57px; /* padding-top for .main-content plus 5px */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@media (max-width: 991px) {
|
|
|
|
:global(.compose-box-button-sticky, .compose-box-button-fixed) {
|
|
|
|
top: 62px; /* padding-top for .main-content plus 10px */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@supports (-webkit-overflow-scrolling: touch) {
|
|
|
|
.compose-box-button-sticky {
|
|
|
|
/* disable sticky positioning on iOS due to
|
|
|
|
https://github.com/nolanlawson/pinafore/issues/667 */
|
|
|
|
position: relative;
|
|
|
|
z-index: 0;
|
|
|
|
top: 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</style>
|
|
|
|
<script>
|
|
|
|
import ComposeButton from './ComposeButton.html'
|
|
|
|
import { store } from '../../_store/store'
|
|
|
|
import { importShowComposeDialog } from '../dialog/asyncDialogs'
|
|
|
|
import { observe } from 'svelte-extras'
|
|
|
|
|
|
|
|
const USE_IOS_WORKAROUND = process.browser && CSS.supports('-webkit-overflow-scrolling', 'touch')
|
|
|
|
|
|
|
|
export default {
|
|
|
|
oncreate () {
|
2019-08-11 18:09:43 +00:00
|
|
|
this.setupIntersectionObservers()
|
2018-12-02 19:22:18 +00:00
|
|
|
this.setupIOSWorkaround()
|
|
|
|
},
|
|
|
|
ondestroy () {
|
2019-08-11 18:09:43 +00:00
|
|
|
this.teardownIntersectionObservers()
|
2018-12-02 19:22:18 +00:00
|
|
|
},
|
|
|
|
store: () => store,
|
|
|
|
data: () => ({
|
|
|
|
sticky: false
|
|
|
|
}),
|
|
|
|
computed: {
|
|
|
|
timelineInitialized: ({ $timelineInitialized }) => $timelineInitialized
|
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
observe,
|
|
|
|
onClickButton () {
|
2019-08-03 20:49:37 +00:00
|
|
|
const { sticky } = this.get()
|
2018-12-02 19:22:18 +00:00
|
|
|
if (sticky) {
|
|
|
|
// when the button is sticky, we're scrolled down the home timeline,
|
|
|
|
// so we should launch a new compose dialog
|
|
|
|
this.showDialog()
|
|
|
|
} else {
|
|
|
|
// else we're actually posting a new toot, let our parent know
|
|
|
|
this.fire('postAction')
|
|
|
|
}
|
|
|
|
},
|
|
|
|
async showDialog () {
|
|
|
|
(await importShowComposeDialog())()
|
|
|
|
},
|
|
|
|
setupIOSWorkaround () {
|
|
|
|
// This is an elaborate fix for https://github.com/nolanlawson/pinafore/issues/667
|
|
|
|
// We detect iOS using support for -webkit-overflow-scrolling: touch
|
|
|
|
// (both here and in global.scss). Then, we set the main content element
|
|
|
|
// to be overflow-x: hidden, which normally would break the sticky button
|
|
|
|
// because its parent is now the scrolling context. So for iOS only, we
|
|
|
|
// create a fake sticky button by listening to intersecting events
|
|
|
|
// and inserting a permanently fixed-position element into the DOM.
|
2019-08-03 20:49:37 +00:00
|
|
|
const { showSticky } = this.get()
|
2018-12-02 19:22:18 +00:00
|
|
|
if (!USE_IOS_WORKAROUND || !showSticky) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-08-03 20:49:37 +00:00
|
|
|
const cleanup = () => {
|
|
|
|
const existingElement = document.getElementById('the-sticky-button')
|
2018-12-02 19:22:18 +00:00
|
|
|
if (existingElement) {
|
|
|
|
document.body.removeChild(existingElement)
|
|
|
|
}
|
|
|
|
if (this.__fixedStickyButton) {
|
|
|
|
this.__fixedStickyButton.destroy()
|
|
|
|
this.__fixedStickyButton = null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-03 20:49:37 +00:00
|
|
|
const createFixedStickyButton = () => {
|
|
|
|
const element = document.createElement('div')
|
2018-12-02 19:22:18 +00:00
|
|
|
element.setAttribute('id', 'the-sticky-button')
|
|
|
|
element.classList.add('compose-box-button-wrapper')
|
|
|
|
element.classList.add('compose-box-button-fixed')
|
|
|
|
document.body.appendChild(element)
|
2019-08-03 20:49:37 +00:00
|
|
|
const rect = this.refs.wrapper.getBoundingClientRect()
|
2018-12-02 19:22:18 +00:00
|
|
|
Object.assign(element.style, {
|
|
|
|
left: `${rect.left}px`,
|
|
|
|
position: 'fixed'
|
|
|
|
})
|
|
|
|
this.__fixedStickyButton = new ComposeButton({
|
|
|
|
target: element,
|
|
|
|
data: {
|
|
|
|
sticky: true,
|
|
|
|
overLimit: false
|
|
|
|
}
|
|
|
|
})
|
|
|
|
this.__fixedStickyButton.on('click', () => this.showDialog())
|
|
|
|
}
|
|
|
|
|
|
|
|
this.observe('sticky', sticky => {
|
|
|
|
cleanup()
|
|
|
|
if (sticky) {
|
|
|
|
createFixedStickyButton()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
this.on('destroy', () => cleanup())
|
|
|
|
},
|
2019-08-11 18:09:43 +00:00
|
|
|
setupIntersectionObservers () {
|
2019-08-03 20:49:37 +00:00
|
|
|
const sentinel = this.refs.sentinel
|
2018-12-02 19:22:18 +00:00
|
|
|
|
|
|
|
this.__stickyObserver = new IntersectionObserver(entries => this.onObserve(entries))
|
|
|
|
this.__stickyObserver.observe(sentinel)
|
|
|
|
|
|
|
|
// also create a one-shot observer for the $timelineInitialized event,
|
|
|
|
// due to a bug in Firefox where when the scrollTop is set
|
|
|
|
// manually, the other observer doesn't necessarily fire
|
|
|
|
this.observe('timelineInitialized', timelineInitialized => {
|
2019-08-11 18:09:43 +00:00
|
|
|
if (timelineInitialized && !this.__oneShotObserver) {
|
|
|
|
this.__oneShotObserver = new IntersectionObserver(entries => {
|
2018-12-02 19:22:18 +00:00
|
|
|
this.onObserve(entries)
|
2019-08-11 18:09:43 +00:00
|
|
|
this.__oneShotObserver.disconnect()
|
|
|
|
this.__oneShotObserver = null
|
2018-12-02 19:22:18 +00:00
|
|
|
})
|
2019-08-11 18:09:43 +00:00
|
|
|
this.__oneShotObserver.observe(sentinel)
|
2018-12-02 19:22:18 +00:00
|
|
|
}
|
|
|
|
}, { init: false })
|
|
|
|
},
|
|
|
|
onObserve (entries) {
|
|
|
|
this.set({ sticky: !entries[0].isIntersecting })
|
|
|
|
},
|
2019-08-11 18:09:43 +00:00
|
|
|
teardownIntersectionObservers () {
|
2018-12-02 19:22:18 +00:00
|
|
|
if (this.__stickyObserver) {
|
|
|
|
this.__stickyObserver.disconnect()
|
2019-08-11 18:09:43 +00:00
|
|
|
this.__stickyObserver = null
|
|
|
|
}
|
|
|
|
if (this.__oneShotObserver) {
|
|
|
|
this.__oneShotObserver.disconnect()
|
|
|
|
this.__oneShotObserver = null
|
2018-12-02 19:22:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
components: {
|
|
|
|
ComposeButton
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</script>
|