Class: HakumiComponents::FloatButton::PositionConfig

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
app/components/hakumi_components/float_button/position_config.rb

Constant Summary collapse

PLACEMENTS =
T.let({
  bottom_right: { bottom: 24, right: 24 },
  bottom_left: { bottom: 24, left: 24 },
  top_right: { top: 24, right: 24 },
  top_left: { top: 24, left: 24 }
}.freeze, T::Hash[Symbol, T::Hash[Symbol, Integer]])
OffsetValue =
T.type_alias { T.nilable(T.any(Integer, String)) }
OffsetMap =
T.type_alias { T::Hash[Symbol, OffsetValue] }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(floating:, placement:, offset: nil) ⇒ PositionConfig

Returns a new instance of PositionConfig.



20
21
22
23
24
25
# File 'app/components/hakumi_components/float_button/position_config.rb', line 20

def initialize(floating:, placement:, offset: nil)
  @floating = T.let(floating, T::Boolean)
  @placement = T.let(placement, Symbol)
  @offset = T.let(offset, T.nilable(OffsetMap))
  validate_placement!
end

Instance Attribute Details

#floatingObject (readonly)

Returns the value of attribute floating.



28
29
30
# File 'app/components/hakumi_components/float_button/position_config.rb', line 28

def floating
  @floating
end

#placementObject (readonly)

Returns the value of attribute placement.



31
32
33
# File 'app/components/hakumi_components/float_button/position_config.rb', line 31

def placement
  @placement
end

Instance Method Details

#resolved_offsetObject



47
48
49
50
51
# File 'app/components/hakumi_components/float_button/position_config.rb', line 47

def resolved_offset
  base = T.let(PLACEMENTS.fetch(@placement).dup, OffsetMap)
  extra = @offset || {}
  base.merge(extra).compact
end

#styleObject



34
35
36
37
38
39
40
41
42
43
44
# File 'app/components/hakumi_components/float_button/position_config.rb', line 34

def style
  return nil unless @floating

  segments = T.let([ "position: fixed", "z-index: 1000" ], T::Array[String])
  resolved_offset.each do |key, value|
    next if value.nil?

    segments << "#{key}: #{dimension(value)}"
  end
  segments.join("; ")
end