Skip to content

Commit

Permalink
#23 mdc-slider
Browse files Browse the repository at this point in the history
  • Loading branch information
mpetuska committed Dec 30, 2021
1 parent c73d7f9 commit 9609cf9
Show file tree
Hide file tree
Showing 22 changed files with 666 additions and 119 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* N/A
### New modules wrapped
- [x] mdc-switch
- [x] mdc-slider

# 0.0.2
## Versions
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ fun Sample() {

### Progress

Here's a tracker list of currently completed [material-components-web] modules (25/49):
Here's a tracker list of currently completed [material-components-web] modules (26/49):

- [ ] mdc-animation (SASS)
- [x] mdc-auto-init (won't wrap)
Expand Down Expand Up @@ -110,7 +110,7 @@ Here's a tracker list of currently completed [material-components-web] modules (
- [x] mdc-segmented-button
- [ ] mdc-select
- [ ] mdc-shape (SASS)
- [ ] mdc-slider
- [x] mdc-slider
- [x] mdc-snackbar
- [x] mdc-switch
- [ ] mdc-tab-bar
Expand Down
8 changes: 4 additions & 4 deletions kmdc/kmdc-chips/src/jsMain/kotlin/MDCChipSet.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import org.w3c.dom.Element
private external val MDCChipsCSS: dynamic

@JsModule("@material/chips")
private external object MDCChipsModule {
class MDCChipSet(element: Element) {
companion object {
fun attachTo(element: Element)
public external object MDCChipsModule {
public class MDCChipSet(element: Element) {
public companion object {
public fun attachTo(element: Element): MDCChipSet
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions kmdc/kmdc-core/src/jsMain/kotlin/MDCDsl.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package dev.petuska.kmdc.core

@DslMarker
@MDCInternalDsl
public annotation class MDCDsl

@DslMarker
@MDCInternalDsl
public annotation class MDCAttrsDsl

@DslMarker
Expand Down
103 changes: 8 additions & 95 deletions kmdc/kmdc-segmented-button/src/jsMain/kotlin/MDCSegmentedButton.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,100 +3,27 @@ package dev.petuska.kmdc.segmented.button
import androidx.compose.runtime.Composable
import dev.petuska.kmdc.core.Builder
import dev.petuska.kmdc.core.ComposableBuilder
import dev.petuska.kmdc.core.MDCAttrsDsl
import dev.petuska.kmdc.core.MDCDsl
import dev.petuska.kmdc.core.mdc
import dev.petuska.kmdc.ripple.MDCRippleModule
import org.jetbrains.compose.web.attributes.AttrsBuilder
import org.jetbrains.compose.web.dom.AttrBuilderContext
import org.jetbrains.compose.web.dom.Div
import org.jetbrains.compose.web.dom.ElementScope
import org.w3c.dom.Element
import org.w3c.dom.HTMLButtonElement
import org.w3c.dom.HTMLDivElement
import org.w3c.dom.events.Event

@JsModule("@material/segmented-button/dist/mdc.segmented-button.css")
private external val MDCSegmentedButtonStyle: dynamic

@JsModule("@material/segmented-button")
public external object MDCSegmentedButtonModule {
public class MDCSegmentedButton(element: Element) {
public companion object {
public fun attachTo(element: Element): MDCSegmentedButton
}

public val segments: Array<MDCSegmentedButtonSegment>
public fun initialize(
segmentFactory: (
el: Element,
foundation: dynamic
) -> MDCSegmentedButtonSegment = definedExternally
)

public fun initialSyncWithDOM()
public fun destroy()
public fun getDefaultFoundation(): dynamic
public fun getSelectedSegments(): Array<dynamic>
public fun selectSegment(indexOrSegmentId: dynamic)
public fun unselectSegment(indexOrSegmentId: dynamic)
public fun isSegmentSelected(indexOrSegmentId: dynamic): Boolean
}

public class MDCSegmentedButtonSegment(element: Element) {
public companion object {
public fun attachTo(element: Element): MDCSegmentedButtonSegment
}

public var ripple: MDCRippleModule.MDCRipple
public fun initialize(rippleFactory: (el: Element, foundation: dynamic) -> MDCRippleModule.MDCRipple)
public fun initialSyncWithDOM()
public fun destroy()
public fun getDefaultFoundation(): dynamic
public fun setIndex(index: Number)
public fun setIsSingleSelect(isSingleSelect: Boolean)
public fun isSelected(): Boolean
public fun setSelected()
public fun setUnselected()
public fun getSegmentId(): dynamic
}

public interface SegmentDetail {
public val index: Number
public val selected: Boolean
public val segmentId: String?
}

public class MDCSegmentedButtonChangeEvent : Event {
public val detail: SegmentDetail
}

public class MDCSegmentedButtonSegmentSelectedEvent : Event {
public val detail: SegmentDetail
}
}

public data class MDCSegmentedButtonOpts(
var singleSelect: Boolean = false,
)

public class MDCSegmentedButtonScope(scope: ElementScope<HTMLDivElement>, internal val opts: MDCSegmentedButtonOpts) :
ElementScope<HTMLDivElement> by scope {

/**
* [JS API](https://github.com/material-components/material-components-web/tree/v13.0.0/packages/mdc-segmented-button)
*/
@MDCAttrsDsl
public fun AttrsBuilder<HTMLButtonElement>.onSegmentSelected(
listener: (MDCSegmentedButtonModule.MDCSegmentedButtonSegmentSelectedEvent) -> Unit
) {
// TODO Uncomment after https://github.com/material-components/material-components-web/issues/7127 is fixed
// addEventListener("MDCSegmentedButtonSegment:selected") {
addEventListener("selected") {
listener(it.nativeEvent.unsafeCast<MDCSegmentedButtonModule.MDCSegmentedButtonSegmentSelectedEvent>())
}
}
}
public class MDCSegmentedButtonAttrsScope private constructor() : AttrsBuilder<HTMLButtonElement>()
public class MDCSegmentedButtonScope(
scope: ElementScope<HTMLDivElement>,
internal val options: MDCSegmentedButtonOpts
) :
ElementScope<HTMLDivElement> by scope

/**
* [JS API](https://github.com/material-components/material-components-web/tree/v13.0.0/packages/mdc-segmented-button)
Expand All @@ -105,7 +32,7 @@ public class MDCSegmentedButtonScope(scope: ElementScope<HTMLDivElement>, intern
@Composable
public fun MDCSegmentedButton(
opts: Builder<MDCSegmentedButtonOpts>? = null,
attrs: AttrBuilderContext<HTMLDivElement>? = null,
attrs: Builder<MDCSegmentedButtonAttrsScope>? = null,
content: ComposableBuilder<MDCSegmentedButtonScope>? = null
) {
MDCSegmentedButtonStyle
Expand All @@ -126,22 +53,8 @@ public fun MDCSegmentedButton(
it.mdc<MDCSegmentedButtonModule.MDCSegmentedButton> { destroy() }
}
}
attrs?.invoke(this)
attrs?.invoke(this.unsafeCast<MDCSegmentedButtonAttrsScope>())
},
content = content?.let { { MDCSegmentedButtonScope(this, options).it() } }
)
}

/**
* [JS API](https://github.com/material-components/material-components-web/tree/v13.0.0/packages/mdc-segmented-button)
*/
@MDCAttrsDsl
public fun AttrsBuilder<HTMLDivElement>.onSegmentChange(
listener: (MDCSegmentedButtonModule.MDCSegmentedButtonChangeEvent) -> Unit
) {
// TODO Uncomment after https://github.com/material-components/material-components-web/issues/7127 is fixed
// addEventListener("MDCSegmentedButton:change") {
addEventListener("change") {
listener(it.nativeEvent.unsafeCast<MDCSegmentedButtonModule.MDCSegmentedButtonChangeEvent>())
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package dev.petuska.kmdc.segmented.button

import dev.petuska.kmdc.core.MDCEvent
import dev.petuska.kmdc.ripple.MDCRippleModule
import org.w3c.dom.Element

@JsModule("@material/segmented-button")
public external object MDCSegmentedButtonModule {
public class MDCSegmentedButton(element: Element) {
public companion object {
public fun attachTo(element: Element): MDCSegmentedButton
}

public val segments: Array<MDCSegmentedButtonSegment>
public fun initialize(
segmentFactory: (
el: Element,
foundation: dynamic
) -> MDCSegmentedButtonSegment = definedExternally
)

public fun initialSyncWithDOM()
public fun destroy()
public fun getDefaultFoundation(): dynamic
public fun getSelectedSegments(): Array<dynamic>
public fun selectSegment(indexOrSegmentId: dynamic)
public fun unselectSegment(indexOrSegmentId: dynamic)
public fun isSegmentSelected(indexOrSegmentId: dynamic): Boolean
}

public class MDCSegmentedButtonSegment(element: Element) {
public companion object {
public fun attachTo(element: Element): MDCSegmentedButtonSegment
}

public var ripple: MDCRippleModule.MDCRipple
public fun initialize(rippleFactory: (el: Element, foundation: dynamic) -> MDCRippleModule.MDCRipple)
public fun initialSyncWithDOM()
public fun destroy()
public fun getDefaultFoundation(): dynamic
public fun setIndex(index: Number)
public fun setIsSingleSelect(isSingleSelect: Boolean)
public fun isSelected(): Boolean
public fun setSelected()
public fun setUnselected()
public fun getSegmentId(): dynamic
}

public interface SegmentDetail {
public val index: Number
public val selected: Boolean
public val segmentId: String?
}

public class MDCSegmentedButtonEvent : MDCEvent<SegmentDetail>

public class MDCSegmentedButtonSegmentEvent : MDCEvent<SegmentDetail>
public class MDCSegmentedButtonSegmentClickEvent : MDCEvent<Number>
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import androidx.compose.runtime.Composable
import dev.petuska.kmdc.core.Builder
import dev.petuska.kmdc.core.ComposableBuilder
import dev.petuska.kmdc.core.MDCDsl
import org.jetbrains.compose.web.dom.AttrBuilderContext
import org.jetbrains.compose.web.attributes.AttrsBuilder
import org.jetbrains.compose.web.dom.Button
import org.jetbrains.compose.web.dom.Div
import org.jetbrains.compose.web.dom.ElementScope
Expand All @@ -15,6 +15,7 @@ public data class MDCSegmentedButtonSegmentOpts(
var touch: Boolean = false
)

public class MDCSegmentedButtonSegmentAttrsScope private constructor() : AttrsBuilder<HTMLButtonElement>()
public class MDCSegmentedButtonSegmentScope(scope: ElementScope<HTMLButtonElement>) :
ElementScope<HTMLButtonElement> by scope

Expand All @@ -25,7 +26,7 @@ public class MDCSegmentedButtonSegmentScope(scope: ElementScope<HTMLButtonElemen
@Composable
public fun MDCSegmentedButtonScope.MDCSegmentedButtonSegment(
opts: Builder<MDCSegmentedButtonSegmentOpts>? = null,
attrs: AttrBuilderContext<HTMLButtonElement>? = null,
attrs: Builder<MDCSegmentedButtonSegmentAttrsScope>? = null,
content: ComposableBuilder<MDCSegmentedButtonSegmentScope>? = null,
) {
val options = MDCSegmentedButtonSegmentOpts().apply { opts?.invoke(this) }
Expand All @@ -38,13 +39,13 @@ public fun MDCSegmentedButtonScope.MDCSegmentedButtonSegment(
if (options.selected) {
classes("mdc-segmented-button__segment--selected")
}
if (this@MDCSegmentedButtonSegment.opts.singleSelect) {
if (this@MDCSegmentedButtonSegment.options.singleSelect) {
attr("aria-checked", "${options.selected}")
attr("role", "radio")
} else {
attr("aria-pressed", "${options.selected}")
}
attrs?.invoke(this)
attrs?.invoke(this.unsafeCast<MDCSegmentedButtonSegmentAttrsScope>())
}
) {
if (options.touch) {
Expand All @@ -65,7 +66,7 @@ public fun MDCSegmentedButtonScope.MDCSegmentedButtonSegment(
public fun MDCSegmentedButtonScope.MDCSegmentedButtonSegment(
text: String,
opts: Builder<MDCSegmentedButtonSegmentOpts>? = null,
attrs: AttrBuilderContext<HTMLButtonElement>? = null,
attrs: Builder<MDCSegmentedButtonSegmentAttrsScope>? = null,
) {
MDCSegmentedButtonSegment(opts, attrs) {
MDCSegmentedButtonLabel(text)
Expand Down
69 changes: 69 additions & 0 deletions kmdc/kmdc-segmented-button/src/jsMain/kotlin/events.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package dev.petuska.kmdc.segmented.button

import dev.petuska.kmdc.core.MDCAttrsDsl

@JsModule("@material/segmented-button/segmented-button/constants")
private external object SegmentedButtonModuleConstants {
@Suppress("ClassName")
object events {
val CHANGE: String
val SELECTED: String
}
}

/**
* [JS API](https://github.com/material-components/material-components-web/tree/v13.0.0/packages/mdc-segmented-button)
*/
@MDCAttrsDsl
public fun MDCSegmentedButtonAttrsScope.onSegmentSelected(
listener: (event: MDCSegmentedButtonModule.MDCSegmentedButtonEvent) -> Unit
) {
addEventListener(SegmentedButtonModuleConstants.events.SELECTED) {
listener(it.nativeEvent.unsafeCast<MDCSegmentedButtonModule.MDCSegmentedButtonEvent>())
}
}

/**
* [JS API](https://github.com/material-components/material-components-web/tree/v13.0.0/packages/mdc-segmented-button)
*/
@MDCAttrsDsl
public fun MDCSegmentedButtonAttrsScope.onSegmentChange(
listener: (event: MDCSegmentedButtonModule.MDCSegmentedButtonEvent) -> Unit
) {
addEventListener(SegmentedButtonModuleConstants.events.CHANGE) {
listener(it.nativeEvent.unsafeCast<MDCSegmentedButtonModule.MDCSegmentedButtonEvent>())
}
}

@JsModule("@material/segmented-button/segment/constants")
private external object SegmentModuleConstants {
@Suppress("ClassName")
object events {
val CLICK: String
val SELECTED: String
}
}

/**
* [JS API](https://github.com/material-components/material-components-web/tree/v13.0.0/packages/mdc-segmented-button)
*/
@MDCAttrsDsl
public fun MDCSegmentedButtonSegmentAttrsScope.onSegmentSelected(
listener: (event: MDCSegmentedButtonModule.MDCSegmentedButtonSegmentEvent) -> Unit
) {
addEventListener(SegmentModuleConstants.events.SELECTED) {
listener(it.nativeEvent.unsafeCast<MDCSegmentedButtonModule.MDCSegmentedButtonSegmentEvent>())
}
}

/**
* [JS API](https://github.com/material-components/material-components-web/tree/v13.0.0/packages/mdc-segmented-button)
*/
@MDCAttrsDsl
public fun MDCSegmentedButtonSegmentAttrsScope.onSegmentClick(
listener: (event: MDCSegmentedButtonModule.MDCSegmentedButtonSegmentClickEvent) -> Unit
) {
addEventListener(SegmentModuleConstants.events.CLICK) {
listener(it.nativeEvent.unsafeCast<MDCSegmentedButtonModule.MDCSegmentedButtonSegmentClickEvent>())
}
}
19 changes: 19 additions & 0 deletions kmdc/kmdc-slider/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import util.mdcVersion

plugins {
id("plugin.library-compose")
id("plugin.publishing-mpp")
}

description = "Compose Multiplatform Kotlin/JS wrappers for @material/slider"

kotlin {
sourceSets {
jsMain {
dependencies {
api(project(":kmdc:kmdc-core"))
api(npm("@material/slider", mdcVersion))
}
}
}
}
Loading

0 comments on commit 9609cf9

Please sign in to comment.