pleroma-fe/src/components/tab_switcher/tab_switcher.jsx

183 lines
4.7 KiB
React
Raw Normal View History

2021-04-25 13:23:16 +03:00
// eslint-disable-next-line no-unused
2022-03-21 21:29:51 +02:00
import { h, Fragment } from 'vue'
2023-04-05 21:06:37 -06:00
import { mapState } from 'pinia'
2020-10-20 22:54:43 +03:00
import { FontAwesomeIcon as FAIcon } from '@fortawesome/vue-fontawesome'
import './tab_switcher.scss'
2025-02-03 13:02:14 +02:00
import { useInterfaceStore } from 'src/stores/interface'
2026-01-06 16:22:52 +02:00
const findFirstUsable = (slots) => slots.findIndex((_) => _.props)
export default {
name: 'TabSwitcher',
props: {
renderOnlyFocused: {
required: false,
type: Boolean,
2026-01-06 16:22:52 +02:00
default: false,
},
onSwitch: {
required: false,
2019-11-08 11:14:01 -05:00
type: Function,
2026-01-06 16:22:52 +02:00
default: undefined,
},
activeTab: {
required: false,
2019-11-08 11:14:01 -05:00
type: String,
2026-01-06 16:22:52 +02:00
default: undefined,
},
scrollableTabs: {
required: false,
type: Boolean,
2026-01-06 16:22:52 +02:00
default: false,
2020-05-03 17:36:12 +03:00
},
bodyScrollLock: {
required: false,
type: Boolean,
2026-01-06 16:22:52 +02:00
default: false,
},
},
2026-01-06 16:22:52 +02:00
data() {
return {
2026-01-06 16:22:52 +02:00
active: findFirstUsable(this.slots()),
}
},
2019-08-10 14:48:05 -04:00
computed: {
2026-01-06 16:22:52 +02:00
activeIndex() {
2019-08-10 14:48:05 -04:00
// In case of controlled component
if (this.activeTab) {
2026-01-06 16:22:52 +02:00
return this.slots().findIndex(
(slot) => slot && slot.props && this.activeTab === slot.props.key,
)
2019-08-10 14:48:05 -04:00
} else {
return this.active
}
},
2026-01-06 16:22:52 +02:00
isActive() {
return (tabName) => {
const isWanted = (slot) =>
slot.props && slot.props['data-tab-name'] === tabName
2022-03-27 12:58:28 +03:00
return this.$slots.default().findIndex(isWanted) === this.activeIndex
}
2026-01-06 16:22:52 +02:00
},
2019-08-10 14:48:05 -04:00
},
2026-01-06 16:22:52 +02:00
beforeUpdate() {
2021-04-18 15:03:28 +03:00
const currentSlot = this.slots()[this.active]
2021-04-25 13:23:16 +03:00
if (!currentSlot.props) {
2021-04-18 15:03:28 +03:00
this.active = findFirstUsable(this.slots())
2019-07-05 10:02:14 +03:00
}
},
methods: {
2026-01-06 16:22:52 +02:00
clickTab(index) {
return (e) => {
e.preventDefault()
this.setTab(index)
}
},
2021-04-18 15:03:28 +03:00
// DO NOT put it to computed, it doesn't work (caching?)
2026-01-06 16:22:52 +02:00
slots() {
2022-03-21 21:29:51 +02:00
if (this.$slots.default()[0].type === Fragment) {
return this.$slots.default()[0].children
}
2022-03-18 13:32:36 +02:00
return this.$slots.default()
2021-04-18 15:03:28 +03:00
},
2026-01-06 16:22:52 +02:00
setTab(index) {
if (typeof this.onSwitch === 'function') {
2021-04-18 15:03:28 +03:00
this.onSwitch.call(null, this.slots()[index].key)
}
this.active = index
if (this.scrollableTabs) {
this.$refs.contents.scrollTop = 0
}
2026-01-06 16:22:52 +02:00
},
},
2026-01-06 16:22:52 +02:00
render() {
const tabs = this.slots().map((slot, index) => {
const props = slot.props
if (!props) return
const classesTab = ['tab']
const classesWrapper = ['tab-wrapper']
if (this.activeIndex === index) {
classesTab.push('active')
classesWrapper.push('active')
}
if (props.image) {
2019-07-05 10:02:14 +03:00
return (
2019-08-10 00:26:29 -04:00
<div class={classesWrapper.join(' ')}>
2019-07-05 10:02:14 +03:00
<button
disabled={props.disabled}
onClick={this.clickTab(index)}
2020-05-25 16:10:14 +03:00
class={classesTab.join(' ')}
type="button"
2023-03-02 21:15:43 -05:00
role="tab"
2020-05-25 16:10:14 +03:00
>
2026-01-06 16:22:52 +02:00
<img src={props.image} title={props['image-tooltip']} />
{props.label ? '' : props.label}
2020-05-25 16:10:14 +03:00
</button>
2019-07-05 10:02:14 +03:00
</div>
)
2026-01-06 16:22:52 +02:00
}
return (
<div class={classesWrapper.join(' ')}>
<button
disabled={props.disabled}
onClick={this.clickTab(index)}
class={classesTab.join(' ')}
type="button"
role="tab"
>
{!props.icon ? (
''
) : (
<FAIcon
class="tab-icon"
size="2x"
fixed-width
icon={props.icon}
/>
)}
<span class="text">{props.label}</span>
</button>
</div>
)
})
2021-04-18 15:03:28 +03:00
const contents = this.slots().map((slot, index) => {
2021-04-25 13:23:16 +03:00
const props = slot.props
if (!props) return
2019-08-10 14:48:05 -04:00
const active = this.activeIndex === index
2026-01-06 16:22:52 +02:00
const classes = [active ? 'active' : 'hidden']
if (props.fullHeight || props['full-height']) {
classes.push('-full-height')
}
if (props.fullWidth || props['full-width']) {
classes.push('-full-width')
}
2024-12-22 17:11:30 +02:00
let delayRender = slot.props['delay-render']
if (delayRender && active) {
slot.props['delay-render'] = false
delayRender = false
}
2026-01-06 16:22:52 +02:00
const renderSlot =
!delayRender && (!this.renderOnlyFocused || active) ? slot : ''
2020-05-28 21:26:33 +03:00
2026-01-06 16:22:52 +02:00
return <div class={classes}>{renderSlot}</div>
})
return (
2026-01-06 16:22:52 +02:00
<div class="tab-switcher top-tabs" ref="root">
<div class="tabs" role="tablist" ref="nav">
2019-01-17 23:05:58 +03:00
{tabs}
</div>
<div
2023-03-02 21:15:43 -05:00
role="tabpanel"
class={'contents' + (this.scrollableTabs ? ' scrollable-tabs' : '')}
v-body-scroll-lock={this.bodyScrollLock}
2025-11-20 12:12:14 +02:00
ref="content"
>
2019-01-17 23:05:58 +03:00
{contents}
</div>
2018-11-21 22:08:27 +03:00
</div>
)
2026-01-06 16:22:52 +02:00
},
}